[swift-evolution] Feedback for SE-0004: Remove the ++ and -- operators

David Zarzycki zarzycki at icloud.com
Sun Dec 6 10:20:04 CST 2015


Hi Jonathan,

This is really cool. Thanks for sharing! As I responded to Chris later in the thread (and to use different words), I do agree that postfix syntax is really useful, and arguably better than prefix syntax. What I was ultimately suggesting was that if Swift only had “unary” operators, then one can freely use them in either postfix or prefix positions. Just use whatever is natural for your code. The only downside of what I was suggesting is that one cannot have *different* behavior for a given operator just based on its prefix or postfix position.

Cheers,
Dave

> On Dec 6, 2015, at 10:40, Jonathan Hull <jhull at gbis.com> wrote:
> 
> TL;DR: Please don’t remove custom postfix operators.
> 
> I actually rely on the custom postfix operators a great deal.
> 
> My first real swift project was to write a PEG parser where you define the grammar rules in swift itself (which allows the rules and code to be intermixed freely… which is super useful). I can call functions or execute a block upon a match.
> 
> The grammar uses a bunch of custom operators including several postfix ones.  Here is a snippet of rules for a grammar which allows complex math equations (This is swift code in a rules() method)
> 
> rule("mathFunc")        <- word & ( %"mathFuncParams" | %"mathEmptyParams")
> rule("mathFuncParams")  <~ "("¡ ~ %"mathExpr" ~ (","¡ ~ %"mathExpr")* ~ ")"¡
> rule("mathEmptyParams") <~ "("¡ ~ ")"¡
> 
> The only reason this works at all is that there are several overloaded operators
> <-		Define rule
> <~		Define rule ignoring whitespace
> &   		Sequence
> ~ 		Same as &, but ignore whitespace
> |		Ordered Choice
> postfix *	Repeat 0 or more times
> postfix ¡	Discard token
> prefix %	Reference to rule
> 
> As mentioned above, this is useful because I can intermix swift code and PEG rules:
> 
> rule("boolCompare") <~ %"mathExpr" ~ %"boolCompareType" ~ %"mathExpr"=^{s in
>     let rhs = s.stack.popMath()!
>     let comp = s.stack.popStr()!
>     let lhs = s.stack.popMath()!
>     s.stack.push(Token.boolExpr(ComparisonExpr(lhs:lhs,rhs:rhs,comparison:comp)))
> }
>         
> (Above: Whenever the rhs mathExpr matches, it runs code which pops the tokens off the stack and replaces them with a token representing the comparison)
> 
> I have several projects which depend on this code, which I wouldn’t be able to do without custom operators (and postfix operators make it a lot closer to the standard PEG notation)
> 
> Please don’t remove custom postfix operators.
> 
> Thanks,
> Jon
> 
> 
> 
>> On Dec 5, 2015, at 11:26 AM, David Zarzycki via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Hi Chris,
>> 
>> An observation about SE-0004: Remove the ++ and — operators:
>> 
>> Given that the above is accepted, the only remaining postfix operator in the language is ‘!’ — and that is implemented by the compiler. Please consider just removing user-defined postfix operators entirely. Doing so would define away a type checking ambiguity where “let f : T -> T = someOperatorIdentifier” is ambiguous when both prefix and postfix operators exist.
>> 
>> Cheers, Dave _____________________________________________ swift-evolution mailing list swift-evolution at swift.org <mailto:swift-evolution at swift.org> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151206/7a869ecf/attachment.html>


More information about the swift-evolution mailing list