Random thought: Alternative stuct

Mike Franklin slavo5150 at yahoo.com
Tue Sep 4 04:03:19 UTC 2018


On Tuesday, 4 September 2018 at 03:38:41 UTC, Nick Sabalausky 
(Abscissa) wrote:
> We have classes and structs:
>
> Classes:
> - Default Storage: GC Heap
> - Indirection Overhead: Yes
> - Semantics: Reference
> - Passed By: Copying the Data's Address
>
> Structs:
> - Default Storage: Stack
> - Indirection Overhead: No
> - Semantics: Value
> - Passed By: Copying the Data (except where the compiler can 
> determine it can safely and more efficiently pass by 
> reference...at least, IIUC)
>
> But we seem to have a lot of need for stuff in-between: emplace 
> for classes, @disable this and move/moveEmplace for structs.
>

Actually there's even more overlap.  `scope`-decorated classes 
are allocated on the stack.  `new`-allocated structs are 
allocated on the heap.  We have both `ref` and pointers for 
reference semantics on value types and structs.

There are also a few additional differences.  classes can inherit 
implementations, but using the technique illustrated in 
https://theartofmachinery.com/2018/08/13/inheritance_and_polymorphism_2.html, you can get something very much like classes.  With `alias this` and multiple-`alias this` (https://github.com/dlang/dmd/pull/8378) it gets even better.

> Just tossing this out there: What if there was a third version, 
> an alternate to struct that:
> - Prohibited implicit copying (perhaps unless the compiler knew 
> the original was never used again?)
> - Was always passed by moving (except where the compiler can 
> determine it can safely and more efficiently pass by reference)?
> - And, after passing it to a function, it would automatically 
> be moved back to the caller (unless compiler can determine it 
> doesn't have to bother).

In my opinion, we shouldn't add a third option.  Rather, we 
should deprecate classes, and make and expand the capabilities of 
structs.  Languages like Zig and Rust have done away with classes 
and all runtime overhead that accompanies them, and are showing 
great promise by expanding on structs with much more composable 
features.

My suggestion:  forget about ProtoObject and investing more into 
the resource pit that is classes.  Just make a better struct and 
supporting features so noone would even want to use classes 
anymore.

> Ie:
> Move-Structs:
> - Default Storage: Stack
> - Indirection Overhead: No
> - Semantics: Reference-like
> - Passed By: Moving the Data (except where compiler...blah blah 
> blah)
>
> IIUC, this would give it the *effect* of reference semantics, 
> but without the indirection (and vtable) overhead, and would 
> allow it to perform RAII cleanup in its dtor when it goes out 
> of scope without ever needing reference counting.
>
> Is this nothing more than reinventing the future "struct with 
> copy constructor and @disable-ed default 
> constructor...and...maybe some other necessary part of the 
> idiom I'm overlooking"?
>
> Is it really just (badly) re-inventing some other XYZ except 
> for differences UVW?
>
> Or would this be a horrible thing to exist?
>
> Any tweaks that would change it from a horrible idea to a 
> fantastic one?
>
> Or is it all just total nonsense?

It's not nonsense, but unless you're willing to invest your time 
and effort into doing something about it, it's equivalent to 
nonsense.

Mike


More information about the Digitalmars-d mailing list