titzer / virgil

A fast and lightweight native programming language
1.2k stars 42 forks source link

Mutability of `def var` fields in subclasses #119

Closed k-sareen closed 1 year ago

k-sareen commented 1 year ago

As I mentioned in https://github.com/titzer/virgil/pull/117#issuecomment-1364495554, I personally find being unable to set def var fields from a subclass unintuitive. Is this a deliberate choice? If it is, can it and its reason be documented somewhere?

Making a setter function is not a good solution, in my opinion, as you've now exposed the setter function to the rest of the world, defeating the purpose of having a def var field. Having a setLength(len: u64); function for a Vector class just so a subclass can set the length sounds like a bad idea as now anyone using the base Vector class can set the length (making the setLength() function private won't help as the subclass can't use private methods either).

titzer commented 1 year ago

I generally prefer avoiding complex interactions between the superclass and subclasses and I like that subclasses aren't granted any special status as far as accessing private fields. It's maybe a bit jarring from the normal Java way, but I ran into too many mysterious and fragile interactions over the years to program in that style anymore.

k-sareen commented 1 year ago

That makes sense. It may be best to mention it in the documentation section about Inheritance then.

titzer commented 1 year ago

Added to the inheritance document.