Feature enhancement: Allow methods on structs, not just classes

3 views
Skip to first unread message

Bill Cox

unread,
Apr 21, 2023, 11:13:00 AM4/21/23
to Rune language discussion
I had hoped to keep struct simple, basically just tuples with named elements.  However, structs appear quite useful, e.g. for tagged unions, and it would be nice to allow simple structs to act like values.  Complex numbers are a good example of when this makes sense.

WDYT?

Aiden Hall

unread,
Apr 24, 2023, 10:23:06 AM4/24/23
to Bill Cox, Rune language discussion
Is there a significant difference between a struct with methods vs a class with its operators overridden? I'm trying to think if there's any difference between structs and classes other than inheritance and having methods. Adding inheritance to structs seems awkward, especially if classes support them (not now but in the future as this isn't implemented yet).

OTOH having a few helper methods is often useful for value classes, but I would prefer there to be some kind of restriction on these struct-methods to create/maintain a meaningful distinction between methods and classes.

On Fri, Apr 21, 2023 at 11:13 AM 'Bill Cox' via Rune language discussion <rune-d...@googlegroups.com> wrote:
I had hoped to keep struct simple, basically just tuples with named elements.  However, structs appear quite useful, e.g. for tagged unions, and it would be nice to allow simple structs to act like values.  Complex numbers are a good example of when this makes sense.

WDYT?

--
You received this message because you are subscribed to the Google Groups "Rune language discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rune-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rune-discuss/CAH9QtQE0v2CkBsVb02wYEMzGa9j_AYiEkePM%3Dyz7MTQUbBbd%2BA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Bill Cox

unread,
Apr 24, 2023, 1:24:05 PM4/24/23
to Aiden Hall, Rune language discussion
Structs are very similar to C structs, but they live on the stack and in globals, and never live in the heap, other than as data members of classes, in which case we allocate an array of the struct type.  They don't have to be reference counted because they use copy semantics.  We can pass large structs to functions via pointers, but this is invisible to the Rune user.  Structs have no constructor or destructor, and instead are constructed with function-call syntax.  In contrast, class instances only live in the heap, allocated in SoA memory layout.

Allowing methods and operator overloading on structs would an efficiency hack, for small objects that would rather be copied by value rather than reference, such as a complex number.

In any case, we don't need this feature for the Rune bootstrap, so I'm voting we put it off for now.

Bill
Reply all
Reply to author
Forward
0 new messages