i'm looking for some help on how to simplify a con...
# codereview
t
i'm looking for some help on how to simplify a concept i've been working on. currently i have a
Validator
class, which can be turned into a
TypedValidator
class by calling
asInt
,
asDouble
, etc: https://github.com/tipsy/javalin/blob/master/src/main/java/io/javalin/validation/Validator.kt the problem is that i have some duplicated functionality that i'd like to get rid of, but i can't think of any (simple) way of doing that
a
You want to de-duplicate those functions?
u
Doesn't asClass<Int> work?
t
yes @Andreas Sinz, that would be nice
a
@tipsy should
T::class.java
be limited to just a couple of types?
t
No, any type should be fine if you use asClass
u
Why do you need asInt? And if you need it, can't you define it as fun asInt = asClass<Int>
t
@uli you can't just cast strings directly, you need to register a converter for each (the API also has to work from java).
asInt()
,
asDouble()
etc are the most common types, which is why i've included them by default
if you need it, can't you define it as fun asInt = asClass<Int>
that i can do! thanks
a
@tipsy what is the difference between
asClass<Int>()
and
asInt() = asClass(Int::class.java)
? is the latter actually needed?
t
asInt()
is a lot easier to read and write than
asClass(Integer.class)
this is also being used from java
a
if you put java in the mix, yes. in a pure kotlin project
asClass<Int>()
is not much worse than
asInt()
t
java is in the mix, sorry ^^
i think i can make the
TypedValidator
a base class, i just haven't quite figured out how yet
got it, thanks to both of you!
a
I would move all the
asClass<..>()
as extension functions. They are only there as an API, and the core of the functionality is implemented anyway in the
fun <T> asClass(clazz: Class<T>): TypedValidator<T>
.