Skip to content

Immutable vectors #152

Open
Open
@jcornaz

Description

@jcornaz
  • ImmutableVector2
  • ImmutableVector3

Hi,

Thank you very much for providing theses utilities for Kotlin :-)

I would like to propose to introduce some immutable classes for at least vector and colors. (could be called KVector2 or ImmutableVector2 for instance)

In order to allow easy usage with LibGDX they could either:

  • inherit from their equivalent in LibGDX, deprecate all mutation method and throw if a mutation method is called
  • provide adapter like ImmutableVector2.asVector2() and Vector2.toImmutableVector2()

Why?
Even in Java I find error-prone that classes like vector or color are mutable. It is for instance possible to change by mistake Vector2.ZERO, and it is a mistake really easy to do. But I think that's even more true in Kotlin where the language provide good support for immutability (val is the default, data class provides copy to easily get a new object, and immutable fields can be smart-casted).

Mutable vectors cannot be safely shared, and one has to do defensive copy each time it gets a vectors, which is cumbersome, unsafe, and inefficient.

In short:

  • Even in Java it should have been immutable
  • In Kotlin, immutable data classes are even more idiomatic

Metadata

Metadata

Assignees

Labels

mathIssues of the ktx-math module

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions