【问题标题】:Swift custom operators with Unicode combining characters具有 Unicode 组合字符的 Swift 自定义运算符
【发布时间】:2020-06-20 19:07:13
【问题描述】:

TL;DR 我可以哄编译器接受组合字符作为后缀运算符吗?

Swift.orgGitHub 以及 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


【解决方案1】:

操作员名称(或任何其他标识符)不能以 U+0302 字符开头。像所有组合标记一样,它是允许的“操作符字符”,但不是允许的“操作符头”。来自“Swift 编程语言”中的Lexical Structure > Operators

运算符语法
运算符 → 运算符头运算符字符opt
...
运算符字符 → U+0300–U+036F

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多