Why are structs and classes so different?

Johan j at j.nl
Tue May 17 17:23:18 UTC 2022


On Monday, 16 May 2022 at 21:20:43 UTC, Ali Çehreli wrote:
> On 5/16/22 10:35, Johan wrote:
>
> > What is very problematic is that you cannot see the
> difference in
> > syntax. In my opinion it would have been much better if the
> language
> > required using a `*` for class types: for example `Foo* a`,
> and `Foo a`
> > would simply give a compile error.
>
> I see. Is it really a high mental load without the syntax? I 
> seems to just work in my programs but perhaps because I am the 
> main programmer and classes are very rare anyway.
>
> Also, same syntax is said to help with template code but 
> perhaps the argument there is a template must be written either 
> for value types or reference types? I am not sure.

`Foo a = b;`
What does that do?
A benefit of statically typed languages is that you know what 
simple code will do. But for this simple statement, you actually 
don't know. Perhaps it is a copy, perhaps not.
I understand that also structs can have reference-like semantics 
with copies if they contain pointers, but it is strange that the 
language at a fundamental basic level has this ambiguity of user 
types.
Indeed the same-syntax template argument is bogus, for exactly 
this reason that you don't know what the code is doing. If the 
language deems it important enough to separate value types from 
reference types, then why does it allow passing _both_ to 
`foo(T)(T t)` ?

-Johan








More information about the Digitalmars-d-learn mailing list