6

C# allows it if you put an @ before the variable name. So int @int = 0; is valid in C#.

Does Haskell have anything similar to this or it doesn't allow it altogether?

Thanks

strider
  • 2,157
  • 5
  • 30
  • 38

2 Answers2

8

Some words are keywords in some contexts but can be freely used as identifiers in others, such as as and hiding.

The C# trick is nothing but just slightly changing the name so that is it no longer a keyword. In Haskell, you could put a _ before or after the name, or append a '.

Martijn
  • 6,713
  • 3
  • 31
  • 38
  • 3
    `_` in the front of a binding has the additional meaning of "don't warn me if I don't use that binding", though. – barsoap Feb 27 '11 at 12:50
  • 1
    In C# the name stays the same, for example `int @a = 0; f(a);` runs f(0). – sdcvvc Feb 27 '11 at 13:14
  • 2
    Try to use '@int' in place of '@a' and you will freak the compiler out when you try to call 'f' with just 'int' and not '@int' – strider Feb 27 '11 at 16:21
  • I thought @int would be how u tell the compiler that the variable's name is "int" e.g. were it a member field of a class, reflection api would show "int" instead of "@int". In Scala u can use backticks to escape: ``val `int` = 5`` – Ustaman Sangat Oct 05 '16 at 14:31
3

It appears that it is not allowed. Note that you can usually put ' after a keyword (since that is a valid identifier character) and get a non-keyword.

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78