是否可以为自定义Swift类型创建自定义运算符语法?

时间:2017-12-13 18:09:57

标签: swift optional syntactic-sugar

我喜欢语法Emulator: libGL error: driver pointer missing Emulator: libGL error: failed to load driver: i965 Emulator: libGL error: unable to load driver: i965_dri.so Emulator: libGL error: driver pointer missing Emulator: libGL error: failed to load driver: i965 Emulator: libGL error: unable to load driver: swrast_dri.so Emulator: libGL error: failed to load driver: swrast Emulator: X Error of failed request: BadValue (integer parameter out of range for operation) Emulator: Major opcode of failed request: 155 (GLX) Emulator: Minor opcode of failed request: 24 (X_GLXCreateNewContext) Emulator: Value in failed request: 0x0 Emulator: Serial number of failed request: 39 Emulator: Current serial number in output stream: 40 Emulator: Process finished with exit code 1 作为Type?的糖。我想尝试利用我自己的类型来利用它。例如,我有一个稍微更加扩展的Optional版本,告诉你为什么某些东西没有值,称为Optional<Type>,我想,例如,我想用{ {1}}。我尝试了以下方法:

DescriptiveOptional<Type>

然后语法就像:

Type~?

还是更短,但我不喜欢它的味道。此外,它让这样的事情变得不可能:

postfix operator ~?



public postfix func ~?<T>(lhs: T.Type) -> DescriptiveOptional<T>.Type {
    return DescriptiveOptional<T>.self
}

那么有没有办法为我的类型制作自己的语法糖?

0 个答案:

没有答案