william
10/22/2018, 11:36 PMping
command would be -n 3
where -n
is the name and 3
is the value. One of the things I am stuck on is writing a parse function on the input that will output a type based on what the argument type. So in this example for -n 3
we would want the value returned to be 3
which is an Int
. In some other cases we would want to parse as a string, boolean, etc. I am not sure how to make this generic to allow for any return types. I thought about doing something like fun <T> parse(text: String, conversion: (String) -> T): T
where I could pass in a conversion function to turn a string into whatever value I wanted but I don't know how to supply a default value to that e.g. if none is provided just return it as a string. Thoughts?nwh
10/22/2018, 11:46 PMnull
is a valid return. In terms of a default case you should probably not have one, except for primitiveswilliam
10/22/2018, 11:48 PMnwh
10/22/2018, 11:49 PMtypes.produce<Int>("123") // returns 123
Then, for the flag system that you're describing, I use data classes to represent the arguments. By using the default constructor and the parameters it contains you get the type that you can pass the bottom layer, and the name of the argumentwilliam
10/22/2018, 11:53 PMnwh
10/22/2018, 11:56 PMwilliam
10/22/2018, 11:58 PMnwh
10/23/2018, 12:01 AMwilliam
10/23/2018, 12:12 AMtypes.produce<Int>("123")
- im just not sure how to access the type from the method?nwh
10/23/2018, 12:14 AMinline
and uses a reified
type. I have another method that's basically types.produce(Int::class, "123")
, which it calls. I'll show you the signature for the reified one thoughinline fun <reified T : Any?> produce(input: String): T? = produce(T::class.java, input)
fun <T : Any?> produce(type: Class<T>, input: String): T?
Using the inline/reified trick is a kotlin specific advantage, it's not much different for this use casewilliam
10/23/2018, 1:47 AMreified
so I will look into that. The second example definately makes sense though i will give it a try. thank youwhen
on the type argument passed in however when I try and return I am getting Type mismatch. Expected T, Given String
or whatever I am trying to return based off of the type passed in. Not sure how to get the return to conform to T
nwh
10/23/2018, 1:55 AMwhen
instead of <T : Any?>
? I'm not familiar with when
william
10/23/2018, 2:06 AMnwh
10/23/2018, 2:07 AMwhere
- it can be used to constrain generic parameters. Can you show the statement?william
10/23/2018, 2:11 AMfun <T : Any> parse(input: String, type: KClass<T>): T {
when (type) {
String::class -> {
return ""
}
...
else -> {
throw UnsupportedOperationException()
}
}
}
nwh
10/23/2018, 2:51 AMwhen (T) {
is String -> return ""
}
Nevermind, this won't work for non-reified types. I think you just have to do an unsafe cast on itThomas
10/23/2018, 8:20 PMwilliam
10/23/2018, 8:56 PMfun <T : Any> parse(input: String, type: KClass<T>): T {
when (type) {
String::class -> return "..." as T
Int::class -> return 0 as T
else -> {
throw UnsupportedOperationException()
}
}
}
but then i get a warning on casting to as T
without checkingnwh
10/23/2018, 9:16 PMwilliam
10/23/2018, 9:19 PMnwh
10/23/2018, 9:24 PM