Swift 具有 Unicode 组合字符的自定义运算符
Swift custom operators with Unicode combining characters
TL;DR
我可以哄编译器接受组合字符作为后缀运算符吗?
Swift.org and GitHub and this useful gist 中的参考文献表明组合字符(例如 U+0300 ff.)可以用作 Swift 中的运算符。
通过明智的实施(此处省略)我可以说“Fiat Lux”
prefix operator ‖ // Find the norm.
postfix operator ‖ // Does nothing.
func / // Scalar division.
允许
let vHat = v / ‖v‖ // Readable as math.
甚至
let v̂ = v / ‖v‖ // Loving it.
我的强迫症现在想像这样使用组合抑扬符作为(topfix)运算符:
let normalizedV = v̂ // Combining char is really a postfix.
所以我跳进去试着写:
postfix operator ^ // Want this to be *combining* circumflex.
postfix func ^(v: Vector) -> Vector { v / ‖v‖ }
并且可以使用普通的旧 U+005E 抑扬符来完成,但是当我尝试使用组合抑扬符 U+0302 时会出现(各种)编译器错误。
运算符名称(或任何其他标识符)不能以 U+0302
字符开头。与所有组合标记一样,它是允许的“运算符字符”,但不是允许的“运算符头”。来自“Swift 编程语言”中的 Lexical Structure > Operators:
GRAMMAR OF OPERATORS
operator → operator-head operator-charactersopt
...
operator-character → U+0300–U+036F
TL;DR 我可以哄编译器接受组合字符作为后缀运算符吗?
Swift.org and GitHub and this useful gist 中的参考文献表明组合字符(例如 U+0300 ff.)可以用作 Swift 中的运算符。
通过明智的实施(此处省略)我可以说“Fiat Lux”
prefix operator ‖ // Find the norm.
postfix operator ‖ // Does nothing.
func / // Scalar division.
允许
let vHat = v / ‖v‖ // Readable as math.
甚至
let v̂ = v / ‖v‖ // Loving it.
我的强迫症现在想像这样使用组合抑扬符作为(topfix)运算符:
let normalizedV = v̂ // Combining char is really a postfix.
所以我跳进去试着写:
postfix operator ^ // Want this to be *combining* circumflex.
postfix func ^(v: Vector) -> Vector { v / ‖v‖ }
并且可以使用普通的旧 U+005E 抑扬符来完成,但是当我尝试使用组合抑扬符 U+0302 时会出现(各种)编译器错误。
运算符名称(或任何其他标识符)不能以 U+0302
字符开头。与所有组合标记一样,它是允许的“运算符字符”,但不是允许的“运算符头”。来自“Swift 编程语言”中的 Lexical Structure > Operators:
GRAMMAR OF OPERATORS
operator → operator-head operator-charactersopt
...
operator-character → U+0300–U+036F