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

Reply via email to