带有Unicode组合字符的Swift自定义运算符

问题描述 投票:1回答:1

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

Swift.orgGitHub以及此useful gist的参考提示,组合字符(例如U + 0300 ff。)可以用作Swift中的运算符。

通过明智的实现(在此省略),我可以说“菲亚特·勒克斯”,并且有

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.

我里面的OCD现在想像这样使用组合的抑扬符作为(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时会遇到(各种)编译器错误。

enter image description here

swift unicode operator-overloading diacritics
1个回答
0
投票

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

操作员语法操作员→操作员头操作员字符opt...操作员字符→U + 0300–U + 036F

© www.soinside.com 2019 - 2024. All rights reserved.