Being able to do this just dawned on me. :heart:
# announcements
k
Being able to do this just dawned on me. ❤️
k
You could also have gone the
MutableList: List
route.
t
You could remove the return type from the fun to reduce code even more
k
@karelpeeters Whats the MutableList route? I'm looking at its code and I don't really get the difference.
Lol I just wanted to be able to share code between a Mutable and Immutable class, which defaults on interfaces allows me to do.
k
You could create separate interfaces for mutable and immutable parts, where the mutable one extends the immutable one.
l
on a side note, wouldn't
longitude in -180..180
be more idiomatic?
k
Huh I thought InteliJ had a quickfix for that.
k
I think the
longitude in -180..180
method creates unnecessary objects
k
It does not, compiler optimizes it away to pretty much the ifs you'd manually write.
l
You should use extension functions instead of interface functions with default implementations to make sure there's no accidental override redefining what valid coordinates are. You could also use
sealed classes
.
k
Making it a nightmare for Java users.
g
Whats the MutableList route
Another part of this, that you don’t need immutable implementation, you just create a builder function that return you immutable interface
Something like this:
Copy code
interface Coordinates {
    val longitude: Double
    val latitude: Double
}

data class MutableCoordinates(
    override val longitude: Double,
    override val latitude: Double
) : Coordinates

fun Coordinates(
    longitude: Double,
    latitude: Double
): Coordinates = MutableCoordinates(
    longitude = longitude,
    latitude = latitude
)
also can add operators for destructuring:
Copy code
inline operator fun Coordinates.component1() = longitude
inline operator fun Coordinates.component2() = latitude