Kilian Koeltzsch - 3 months ago 15

Swift Question

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`

Answer

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:

`.`

`/=-+!*%<>&|^~?`

- Latin-1 supplement:
`¡¢£¤¥¦§©«¬®°±¶»¿×÷`

- General Punctuation:
`‖‗†‡•‣․‥…‧‰‱′″‴‵‶‷‸‹›※‼‽‾⁁⁂⁃⁄⁅⁆⁇⁈⁉⁊⁋⁌⁍⁎⁏⁐⁑⁒⁓⁕⁖⁗⁘⁙⁚⁛⁜⁝⁞`

- Arrows (U+2190 – U+21FF): Whole block
- Mathematical operators (U+2200 – U+22FF): Whole block
- Miscellaneous Technical (U+2300 – U+23FF): Whole block
- Box Drawing (U+2500 – U+257F): Whole block
- Block Elements (U+2580 – U+259F): Whole block
- Geometric Shapes (U+25A0 – U+25FF): Whole block
- Miscellaneous Symbols (U+2600 – U+26FF): Whole block
- Dingbats (U+2700 – U+27BF): All
*except*the circled numbers ❶–❿,➀–➉,➊–➓ - Miscellaneous Math Symbols-A (U+27C0 – U+27EF): Whole block
- Supplemental Arrows-A (U+27F0 – U+27FF): Whole block
- Braille Patterns (U+2800 – U+28FF): Whole block
- Supplemental Arrows-B (U+2900 – U+297F): Whole block
- Miscellaneous Math Symbols-B (U+2980 – U+29FF): Whole block
- Supplemental Math Operators (U+2A00 – U+2AFF): Whole block
- Miscellaneous Symbols and Arrows (U+2B00 – U+2BFF): Whole block
- Supplemental Punctuation (U+2E00 – U+2E7F): Whole block
- CJK Symbols and Punctuation:
`、。〃〈〉《》「」『』【】〒〓〔〕〖〗〘〙〚〛〜〝〞〟〠〡〢〣〤〥〦〧〨〩〪〭〮〯〫〬〰`

The rest of the operator can also be one of these characters:

- Combining Diacritical Marks (U+0300 – U+036F): Whole block
- Combining Diacritical Marks Supplemental (U+1DC0 – U+1DFF): Whole block
- Combining Diacritical Marks for Symbols (U+20D0 – U+20FF): Whole block
- Variation Selectors (U+FE00 – U+FE0F): Whole block
- Combining Half Marks (U+FE20 – U+FE2F): Whole block
- Variation Selectors Supplement (U+E0100 – U+E01FF): Whole block

A

`.`

can appear at the rest of an operator only if the first character is a`.`

.