darkmoon_uk
07/08/2025, 10:57 PMDecimal
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.james
07/09/2025, 2:06 AM-XXLanguage:+CustomEqualsInValueClasses
Seems like it should resolve your compiler error, but I’m unsure if it’ll unlock your use casedarkmoon_uk
07/09/2025, 2:07 AMdarkmoon_uk
07/09/2025, 2:08 AMjames
07/09/2025, 2:08 AMdarkmoon_uk
07/09/2025, 2:14 AMdarkmoon_uk
07/09/2025, 2:40 AMdarkmoon_uk
07/09/2025, 2:40 AMjames
07/09/2025, 8:34 AM