Brackets as part of typ names / compiler messages


(Tino) #1

I hope that Swift 4.0 will have enough template-like syntax to build something like a generic matrix that can be used in a typesafe way, but in the meantime, I wanted to harness the power of unicode to create "class Matrix«4×4»", which really confused the compiler.
"class Vector🐸" is no problem (still have to find a usecase for it, though :), so I wonder about the rationale for disallowing certain characters in names:
I really hope that nothing outside the standard ASCII range is reserved for future use ;-), but an error like "use of character x is forbidden as part of…" imho should be the response.
I guess the reason for the restriction is a harsh countermeasure against funny jokes with similar looking characters, but I'm curious wether this theory is actually true.

Tino


(Brent Royal-Gordon) #2

I hope that Swift 4.0 will have enough template-like syntax to build something like a generic matrix that can be used in a typesafe way, but in the meantime, I wanted to harness the power of unicode to create "class Matrix«4×4»", which really confused the compiler.
"class Vector🐸" is no problem (still have to find a usecase for it, though :), so I wonder about the rationale for disallowing certain characters in names:
I really hope that nothing outside the standard ASCII range is reserved for future use ;-), but an error like "use of character x is forbidden as part of…" imho should be the response.
I guess the reason for the restriction is a harsh countermeasure against funny jokes with similar looking characters, but I'm curious wether this theory is actually true.

Roughly speaking, Swift divides the world of Unicode characters into two categories: “identifier characters” and “operator characters”. (This is discounting a few things like whitespace, but you get the idea.) Just about any character is valid in either an identifier or an operator, but no characters are valid in both. This helps Swift accurately parse your code even though it might contain custom operators.

So if you can’t use « in an identifier, chances are you can use it in an operator. If you can use :frog: in an identifier, then you cannot use it in an operator.

The exact division of characters between these two roles is a little bit idiosyncratic, and I believe the team is looking for someone who knows a lot about Unicode to clean it up. But I doubt there are any plans to abolish the two-role system entirely, because it really helps with parsing.

HTH,

···

--
Brent Royal-Gordon
Architechies