Should a parser type be a struct or class?

Simen Kjærås simen.kjaras at gmail.com
Wed Jun 17 12:32:30 UTC 2020


On Wednesday, 17 June 2020 at 11:50:27 UTC, Per Nordlöw wrote:
> Should a range-compliant aggregate type realizing a parser be 
> encoded as a struct or class? In dmd `Lexer` and `Parser` are 
> both classes.
>
> In general how should I reason about whether an aggregate type 
> should be encoded as a struct or class?

The heuristic I use is 'do I need polymorphism?' If no, it's a 
struct. Another thing that may be worth considering is reference 
semantics. The latter is easy to do with a struct, while 
polymorphism is generally a class-only thing (but check out 
Tardy, which Atila Neves recently posted in the Announce group).

I would say I basically never use classes in D - pointers and 
arrays give me all the reference semantics I need, and 
polymorphism I almost never need.

--
   Simen


More information about the Digitalmars-d-learn mailing list