I wanted to implement a currency operator for use in my software that takes a Double and returns a Currency type for more precise calculations.
The code for a custom operator basically looks somewhat like this. Please disregard the obvious dropping of precision and therefore useless operator in this form.
postfix operator £ { }
postfix func £(number: Double) -> Int {
return Int(number)
}
3.50£ // returns Int(3)
This works fine in Swift. Interestingly enough though I run into errors when trying exactly the same with a € symbol.
postfix operator € { }
postfix func €(number: Double) -> Int {
return Int(number)
}
This produces the error '€' is considered to be an identifier, not an operator
. I don't follow why this isn't allowed though.
The characters allowed in a custom operator is listed in The Swift Programming Language, and € is not one of these. (You could also find corresponding Lexer code).
The main difference between the two currency symbols is that £ (and also ¢ and ¥) are in the Latin-1 supplement block (U+0080 – U+00FF), while € is in the Currency Symbols block (U+20A0 – U+20CF), and for some reason the Swift language considers these as identifier-like instead of operator-like.
The first character of an operator must be one of these:
.
/=-+!*%<>&|^~?
¡¢£¤¥¦§©«¬®°±¶»¿×÷
‖‗†‡•‣․‥…‧‰‱′″‴‵‶‷‸‹›※‼‽‾⁁⁂⁃⁄⁅⁆⁇⁈⁉⁊⁋⁌⁍⁎⁏⁐⁑⁒⁓⁕⁖⁗⁘⁙⁚⁛⁜⁝⁞
、。〃〈〉《》「」『』【】〒〓〔〕〖〗〘〙〚〛〜〝〞〟〠〡〢〣〤〥〦〧〨〩〪〭〮〯〫〬〰
The rest of the operator can also be one of these characters:
A .
can appear at the rest of an operator only if the first character is a .
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With