Mutability - JamesIry/jADT GitHub Wiki

track this issue

I'll retain this page for historical curiosity, but in the end I think mutable by default "final int immutableField" for an immutable field is the only answer that makes sense for a Java target.

JADT's fields are immutable. Which is cool. Except that Java doesn't really make that easy. Zippers and lenses in Java would be just this side of Hades.

So I'd like to make it user optional as to whether a field is immutable or not. I'd love it if immutability was the default, but Java has no keyword that means "mutable". So I either create a new "normal" keyword (thus taking it out of the pool of possible identifiers), use a weird looking keyword that is not a valid Java keywrod, use punctuation that wouldn't be valid in a Java identifier or go with mutable by default.

// Mutable by default
ImmutableADT = ImmutableADT(final int immutableField)

That one has the advantage of being very easy to understand for existing Java devs. But immutable ADTs will be that much noisier.

// Immutable by default, burn a valid identifier
MutableADT = MutableADT(mutable int mutableField)

Easy enough to understand, though backwards from normal Java convention. Mutable ADTs are noisy. Burns a valid identifier (though how often do you want to name something "mutable"?)

// Immutable by default, use a keyword that can't be a Java keyword
MutableADT = MutableADT(#mutable int mutableField)

Same as previouis, but doesn't burn an identifer. And it looks weird.

// Immutable by default, use punctuation
MutableADT = MutableADT(int? mutableField)

Mutable and immutable ADTs aren't too noisy. Looks weird to a Java dev. Also, ? is probably the wrong punctuation - in my mind it implies optionality.

Let the Bikeshedding begin.