[swift-evolution] Allowing Characters for use as Custom Operators

Dennis Lysenko dennis.s.lysenko at gmail.com
Thu Jan 7 10:53:35 CST 2016


Jo, it sounds like you're describing infix functions. If you want to bring
up a concrete example, Kotlin has this implementation and it's fantastic
for creating your own syntactic sugar.

On Thu, Jan 7, 2016, 11:31 AM Félix Cloutier <swift-evolution at swift.org>
wrote:

> In a discussion about adding $ to the operator character set (it currently
> is an identifier character), Chris said that any token must be
> unambiguously either an identifier or an operator even before operators are
> "discovered", and that's why the identifier character sets and operator
> character sets have no overlap right now.
>
> "as" and "is" are not operators, they're keywords that were hard-coded
> into the grammar.
>
> Félix
>
> Le 7 janv. 2016 à 04:31:10, Jo Albright via swift-evolution <
> swift-evolution at swift.org> a écrit :
>
> As my obsession grows with custom operators. I have come across wanting to
> use small words or 1-2 alphabetical characters as custom operators. I
> noticed that “as” and “is” are character based operators and figured it
> wouldn’t hurt to propose the allowance of character based custom operators.
>
> Here are my reasons for allowing them:
>
> 1. easier to read “within” vs “>*<“ or “|*|”
>
> 2. potential opportunity to build an educational library to help explain
> expressions (see below)
>
> infix operator plus { associativity left precedence 200 }
>
> public func plus (lhs: Int, rhs: Int) -> Int {
>
>     return lhs + rhs
>
> }
>
> let totalApples = 5 plus 5
>
> 3. potential to write more like a sentence (this isn’t as high of a need,
> but again a good for entry into the language)
>
> postfix operator oz { }
> postfix operator cup { }
> postfix operator gal { }
>
> public func oz (inout _ lhs: Double) -> Double {
>
>     return lhs
>
> }
>
> public func cup (inout _ lhs: Double) -> Double {
>
>     return lhs *= 8.0
>
> }
>
> public func gal (inout _ lhs: Double) -> Double {
>
>     return lhs *= 128.0
>
> }
>
> let totalLiquidInOunces = 5oz plus 2cup plus 1gal
>
>
> I spent awhile looking to make sure this hasn’t been proposed before. I
> apologize if it is a repeat.
>
> Thanks
>
>  Nerd . Designer . Developer
> Jo Albright
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160107/68a86549/attachment.html>


More information about the swift-evolution mailing list