How to pass generic data types with Safe Args?
Asked Answered
I

1

17

I need to pass Kotlin's Pair as arguments.


<argument android:name="data" app:argType="kotlin.Pair" />
e: CreateCodeFragmentDirections.kt: (16, 15): 2 type arguments expected for class Pair<out A, out B>

<argument android:name="data" app:argType="kotlin.Pair<String, String>" />
Cause: markup not allowed inside attribute value - illegal <  (position: TEXT seen ...<argument android:name="data" app:argType="kotlin.Pair<... @132:30) 

<argument android:name="data" app:argType="kotlin.Pair&lt;String, String&gt;" />
Can't escape identifier `Pair<String, String>` because it contains illegal characters: <>

None of the above attempts worked. How can I do it?

Interval answered 15/10, 2019 at 12:3 Comment(1)
Did you figure a solution for it?Heed
O
0

I think it's not possible to use generic type parameters, so you must fallback to raw generics as in the first of your cases. I'm using java and it compiles fine, however I think your's doesn't because Kotlin is not allowing raw generic usage. Actually I found out that using your last approach with a single type parameter, the generated code contains awful errors. In the end it seems to me generic types are not supported at all, maybe in the future... It would be best to request the feature, in the mean time you can try separating the arguments if applicable or use a non-generic super type (Object ?? --it must be Parcelable/Serializable) with casts or you might want to create your own non-generic Pair class and cast the components only or a specific "StringStringPair" or group all arguments into a single non-generic type ...or switch to java.

Osy answered 11/11, 2020 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.