Greetings everyone,
I recently encountered an issue while working with parametric classes in our language. When attempting to define a parametric class with a mutable field, I received an error message stating, “Mutable members in parametric classes are not yet implemented.” Intrigued by this limitation, I delved deeper into understanding the underlying reasons.
Upon further investigation, I realized that parametric types inherit their subtype relationships (provided they structurally match). For instance, parametric_type(int)
can be considered a subtype of parametric_type(any)
. This introduces a potential risk: one could cast parametric_type(int)
into parametric_type(any)
and then manipulate the mutable field in a way that violates type safety.
Consequently, the language designers disabled mutable fields in parametric classes to prevent such scenarios. However, I propose an alternative solution: instead of prohibiting mutables altogether, we could consider decoupling subtype relationships for parametric classes. By doing so, we would mitigate the risks associated with reinterpret casts while enabling the use of mutable fields.
This approach would enhance the flexibility and usability of parametric classes without compromising type safety. I believe it’s worth exploring this direction further to refine the language’s functionality.
I’m curious to hear your thoughts on this matter. Do you have any alternative suggestions or insights to share?
Looking forward to engaging in fruitful discussions with you all.
Best regards,
Daigorō
( @UltimateLambda )
Edt.: Personally spoken, I would prefer getting rid of type safety instead, but I assume that’s not debatable.