https://kotlinlang.org logo
#getting-started
Title
# getting-started
e

elect

06/24/2022, 6:13 PM
given some constraints, I have to express some logic via boxed primitives It turns out I also have to collect them in a map, but different boxed instance of the same value (ie
java.lang.Boolean(true)
, which returns a boxed Boolean) won't be seen as different keys, that is they would be overwritten how can I solve it?
v

Vampire

06/24/2022, 6:14 PM
Why should they be seen as different keys? A true boolean is a true boolean.
Use something else as key
e

elect

06/24/2022, 6:15 PM
the key is the instance itself, I'll be using referential equality later on
the manual solution is to have two lists, one for the keys, one for the values
v

Vampire

06/24/2022, 6:18 PM
Even referential equality might not help you. Not sure how it is in Kotlin, but in Java you would get the same instance.
y

Youssef Shoaib [MOD]

06/24/2022, 7:17 PM
Box them yourself with something like a
Ref
type. Something like this:
Copy code
sealed class Ref
class IntRef(val value:Int): Ref
class BooleanRef(val value:Boolean): Ref
// etc.
k

Klitos Kyriacou

06/26/2022, 9:37 PM
The expression
java.lang.Boolean(true)
would indeed create a boxed instance with a unique reference. However, this constructor has been deprecated since Java 9, and may be removed in a future release. Also, you would have to use
IdentityHashMap
if you use actual Boolean with different identities. Youssef's suggestion of creating your own boxing class is better. However, it seems a very odd thing to do, using keys that rely on referential identity. What is your use case? It sounds like an X-Y problem.
v

Vampire

06/26/2022, 9:48 PM
Ah, right, somehow thought about
valueOf
e

elect

06/29/2022, 4:32 PM
@Klitos Kyriacou it's a Terraform DSL. Imagine something like this:
Copy code
resource "tls_locally_signed_cert" "example" {
    ...
}
resource "x" "y" {
   booleanProp = tls_locally_signed_cert.example.ready_for_renewal
}
translated in Kotlin to:
Copy code
val example by tls_locally_signed_cert {
    ...
}
val y by x {
   booleanProp = example.ready_for_renewal
}
ready_for_renewal
is a
Boolean
, but under the hood, when I synthesize the Terraform HCL language (first example), I need to retrieve the right string for
booleanProp
at first, I tried to lock that boolean variable to the corresponding string somewhere in a top-level property and that's why my request. But now I'm leaning toward another design, given that those are all delegated props (both left and right), I'm gonna write the string at the top-level for every
getValue
till the next
setValue
, which will grab all the strings (it might be a list) and then clear the top-level container prop
2 Views