== comparison of string literals, and their usage

diniz diniz at posteo.net
Fri Apr 5 14:49:50 UTC 2019


Hello,

Since literal strings are interned (and immutable), can I count on the fact that 
they are compared (==) by pointer?

Context: The use case is a custom lexer for a custom language. I initially 
wanted to represent lexeme classes by a big enum 'LexClass'. However, this makes 
me write 3 times all constant lexemes (keywords and keysigns):
1- in the enum of lexeme classes
2- in an array of constants (for the contant-scanning func)
3- in an associative array mapping constants to their classes
However, if literal strings are compared by equality, then they are kinds of 
Scheme or Ruby symbols: read enum values representing *cases*, which is exactly 
what I need. I would thus use the constants' strings themselves as lexeme 
classes... the parser would not be slown down.

What do you think?
-- 
diniz {la vita e estranj}


More information about the Digitalmars-d-learn mailing list