-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should structs support sub-classing? #2367
Comments
The reasoning above seems to capture it well. Being more restrictive will give us more opportunity to optimize. One may add that:
|
(Not sure if this is the right issue for this, but ...)
In fact, one may want to do that in composite structs themselves, e.g. struct ComplexNumber {
double real;
double im;
}
struct ComplexMatrix2x2 {
ComplexNumber[2] values;
} |
TL;DR: An un-boxable type must not have any concrete subtypes.
It does prompt the question of what happens on an up-cast. Assigning a sub-struct to a super-struct type and allowing unboxing at the super-struct type seems like a guaranteed way to lose the sub-struct data and runtime-type. Unboxing only works when the static type contains all the information needed to rebox the values. (Unless the unboxing is subtype aware and retains all the extra fields too, which seems wasteful.) So, even if a struct type can extend another struct type, to share implementation, it probably shouldn't be assignable to the struct type it extends. Using abstract structs as super-types gives us a way out, by only ever unboxing concrete struct types. This applies to all members, not just the field getters. The members need to be virtual (some override members of In short: Only if we don't introduce a subtype relation, it's entirely for implementation sharing. An un-boxable type must not have any concrete subtypes.
If wo don't have a sub-type relationship, then it's fine. When boxed, and accessed at a supertype interface, we use virtual/interface member access as always.
Same as above.
I think we have plenty of problems inside the same library, if we can solve those sufficiently, my guess is that it will also work across libraries. |
My vote would be against extending concrete structs. My question here would be: what could be a real-world use-case for extending concrete structs? Cause I am struggling to figure out one. It don't think I ever used such extensibility in a language like C++. |
Perhaps the title of this issue should be 'Should concrete structs support sub-classing?'? With that, I believe everybody agrees on "No!" (at least until now ;-). |
Yep, no free lunches. I do think that it's valuable to be able to pass and return things on the stack, since you can sometimes essentially non-speculatively push the boxing point further out, in hopes that you never reach a boxing point. I also would hope that this would make it easier to unbox sub-objects (e.g. represent a field in an object unboxed). For arrays, if we get variance control, I think we should expose a primitive invariant array type, which could serve as the underlying building block for the List type, as well as a tool for writing more low level code. This would at least avoid the issue of having to deal with a List being assigned to a List. You still, of course, have the problem of deciding whether to unbox in the array or not. Ultimately, it might be worth having unboxed tuples to put this in user control when necessary. |
In the struct proposal (#2360), I propose that structs be forbidden from extending concrete structs, though I permit extension of abstract structs. There is a brief discussion of allowing more general extension at the end of the proposal. This issue is to discuss the general question of whether to allow structs to extend other concrete structs, and if so, in what fashion? Concrete questions include:
My proposal was designed to meet certain goals, which are useful to review for this discussion. These are not hard requirements necessarily, but they are relevant to the discussion. Specifically:
The text was updated successfully, but these errors were encountered: