【发布时间】:2020-06-20 19:07:13
【问题描述】:
TL;DR 我可以哄编译器接受组合字符作为后缀运算符吗?
Swift.org 和 GitHub 以及 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 是允许的“操作符”,但不是“操作符头”,即不是操作符的第一个字符
-
啊!你说的对。我阅读
operator-head → U+3008–U+3020但我看到 U+0302. -
你可以回答这个问题,我会把它标记为正确的。即使这个谜团只是我的坏愿景。
标签: swift unicode operator-overloading diacritics