:warning: Does Kotlin (WASM/JS) have a value clas...
# webassembly
d
⚠️ Does Kotlin (WASM/JS) have a value class equality problem? For an App with a financial component, I'm creating a
Decimal
abstraction over: • JVM/Android (
BigDecimal
) iOS (
NSDecimalNumber
) • ...and finally WasmJs (npm's
decimal.js
) ⚠️ In such situations, for the sake of resource efficiency, we would like to use a
value class
to unify these. This is almost working fine - perfectly with JVM/iOS - but theres a problem with equality when it comes to the wasmJs target. Unless I'm mistaken, it works like this:
value class
equality defers to the
equals(other: Any?)
of the wrapped value. ...which for JVM and iOS, is already mapped (as a matter of built-in KMP Interop.) to the canonical function of each platform; being JVM equals and iOS isEqual. With wasmJs (and JS in general I guess?) there is no canonical equality function, which entails that common code
equals
can currently only ever compare object identity for WASM/JS value classes Current 'KMP + value class' design leaves us with no opportunity to defer to a suitable equivalence function (like the
equals
function provided by decimal.js). This breaks use cases for naturally equatable objects. This means the
value class
efficiency dream is off the table ⁉️ Since we can't currently intercept
equals
by either: • Overriding
equals
in a
value class
(compiler error) 🙅 • Nor at the level of declaring an
external class
for JS mapping (an affordance here could be nice) 🙅 (
JsAny
vs
Any
issue) Are there any workarounds? While this may not be a bug in KMP JS/WASM target; it appears to be a significant '_feature usability issue_' for such cases.
j
Hey Chris, do you know about / have you tried the compiler flag
-XXLanguage:+CustomEqualsInValueClasses
Seems like it should resolve your compiler error, but I’m unsure if it’ll unlock your use case
d
Ooh, a Kotlin magic spell! Always a fun day when one of those is required. Sounds like it should do it 😄 Thank you!
😂 3
I'll report back.
j
Don’t forget to do a little dance while saying the magic incantation, or it may not work
😁 2
🕺 3
2
🕺🏾 1
d
Here's the YouTrack for those following along at the back: KT-24874 (Looks like I commented on it 6 years ago too lol)
It worked - Equality unit tests green across all platforms
Thank you @james!!! (And whomever at [JB] is on top of the KT-24874 vs. WasmJs code)
j
Awesome. Glad it worked!