Method parameters and mutable variables

(yes Jayson, I know this thread is stale, but sometimes I drink and respond to old forum posts, deal with it)

I completely agree with you in general. In fact, I would love for an easier way to mark something as read-only rather than to separate the read and write APIs by interface. E.g. List vs MutableList.
Right now it feels like there’s a constant tension point in my code when dealing with purity of an API and performance. When creating games in particular, depending on the platform and whether or not the object is short term, allocation is a huge concern, so there is a lot of recycling objects, so having data classes be immutable isn’t realistic.

The way it is in Kotlin currently, and most languages for that matter, it’s not trivial to mark something as read only. For example:

interface Measurable {
   val size: Vector
}
data class Vector(var x: Float, var y: Float)

Now, if I wanted that to be pure, I’d have to write an interface for just the read portion of Vector, separating Vector and MutableVector, and expose size only through the read-only version. (I don’t really care that someone could unsafely cast the read-only version, the compiler warns them for me)
It would be nice to be able to say something like:

interface Measurable {
   val size: readonly Vector2 // Exposes all component vars as vals
}

I understand that val x: Float could have side-effects, but I’m thinking it could possibly be something special for a data class to have an auto-generated read-only interface.

All that being said, I think my original complaint about method parameters really comes down to that I just don’t like being clever with variable names…

fun layout(width: Float) {
   val w = clamp(width, minWidth, maxWidth) // Now I have width and w... Or maybe I should have named it 'clampedWidth'? Next I want to reduce it by the padding, paddedClampedWidth? Naming is hard...
}

// Or....
fun layout(var width: Float) {
   width = clamp(width, minWidth, maxWidth)
}

I think I picked the most pointless thing to drink and ramble about… I do, however, think it would be nice to be able to auto-create a read-only interface for my data classes. Same with Arrays (without converting them to Lists)