how to read the key value properly: ``` val...
# getting-started
c
how to read the key value properly:
Copy code
val <T> T.key: String
            get() = ""
inside function it is used:
Copy code
@OptIn(ExperimentalStdlibApi::class)
        inline fun <reified T> verify(json: String, publicKey: PublicKey? = null): T {
            val signJSON = Json.decodeFromString<SignJSON>(json)
            val resJson = Json.decodeFromString<T>(signJSON.json)
            var key = publicKey
            if (key == null) {
                key = fromHexString(resJson.key)
            }
            assert(
                verify(
                    signJSON.json.toByteArray(),
                    signJSON.signature.hexToByteArray(),
                    key
                )
            )
            return resJson
        }
Copy code
as I debug into this line to find resJson.key have value , but the access to resJson.key result in empty string `""`
k
You've defined
T.key get()
for any type
T
to return an empty string, so
resJson.key
is therefore empty. Perhaps I've misunderstood your post?
c
the real key is not "", but got "".😀
I don't know how to get the real key value.
k
What actual type is
resJson
? If this type has a property called
key
, your
verify
method doesn't know about it, because you haven't told it what type
resJson
is. If you want it to be generic, it still has to know that it only works on types that have a
key
property. One way to do that is to make whatever the type of
resJson
is, to implement an interface (let's call it
interface HasKey
) and declare your method as
inline fun <reified T: HasKey>
. You should then get rid of your
val <T> T.key
external property as that is unrelated to `resJson`'s type.
c
@Klitos Kyriacou Thanks. That is what I need template variables with partial type info. ✌️