default '==' on structs

spir denis.spir at gmail.com
Wed Feb 2 10:08:18 PST 2011


On 02/02/2011 05:49 PM, Andrej Mitrovic wrote:
> What is Ordinal defined as? If it's a uint, I get the expected results:
>
> alias uint Ordinal;
>
> struct Lexeme {
>     string tag;
>     string slice;
>     Ordinal index;
> }
>
> void main()
> {
>      auto lex1 = Lexeme("a","b",1);
>      auto lex2 = Lexeme("a","b",1);
>
>      assert(lex1 == lex2);
>      assert(lex1 == Lexeme("a","b",1));
> }
>
> Can't say much more without knowing what your app does though.

Actually, its size_t. But I also have everything working fine in a test case 
exactly similar to yours (see other post). Dunno yet why I need to add an 
opEquals just comparing members individually for my unittests to pass.

I take the opportunity to say a few words about the module; case (1) it helps 
debugging (2) some people are interested in it.
The module is a lexing toolkit. It allows creating a lexer from a language's 
"morphology", then use it to scan source. Example for simple arithmetics:

         Morphology morphology = [
             [ "SPACING" ,        `[\ \t]*` ],
             [ "OPEN_GROUP" ,     `(` ],
             [ "CLOSE_GROUP" ,    `)` ],
             [ "operator" ,       `[+*-/]` ],
             [ "symbol" ,         `[a-zA-A][a-zA-A0-9]*` ],
             [ "number" ,         `[+-]?[0-9]+(\.[0-9]+)?` ],
         ];
         auto lexer = new Lexer(morphology);
         auto lexemes = lexer.lexemes(source);

As you see, each lexeme kind is defined by a string tag and a regex format.
The output is an array of lexemes holding the matched slice, wrapped in a class 
LexemeStream. This class mainly provides a match method:
	Lexeme* match (tag)
Match returns a pointer to the current lexeme if it is of the right kind, else 
null (same principle as D's builtin 'in' operator). So, one can either ignore 
the lexeme if all what is needed is testing the match (case of punctuation), or 
use the lexeme's slice (case of values).

The issue I get happens when checking that a result stream of lexemes is as 
expected: '==' failed. I then checked its first/last lexemes only: ditto. Thus, 
I started to wonder about the default semantics of '==' for structs, so that I 
wrote my own opEquals ==> pass for individual lexemes, pass for whole lexeme 
streams. Why? dunno.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list