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

Jo Albright me at jo2.co
Thu Jan 7 03:31:10 CST 2016


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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160107/26917f6c/attachment.html>


More information about the swift-evolution mailing list