With regards to type arguments for a function, is ...
# announcements
j
With regards to type arguments for a function, is there a way not to have to specify all three when calling this function? https://pl.kotl.in/kMoCgjgsg That would be a very handy utility function to have for our codebase but having to specify superfluous type arguments undoes the value.
s
cant you just do
(c as? Foo)?.fooString
?
Shorter too
But to answer your question I dont think so
j
I didn't know about the conditional cast. Tx!
I thought it should be possible: the Kotlin compiler does know the latter two types...
The conditional cast doesn't check at compile time whether it's a subclass unfortunately. So that makes it a rather blunt instrument.
s
What do you mean doesnt check ? I get a warning in this:
Copy code
val foo = ""
(foo as? Int)?.toShort()
j
Aha, I didn't see that warning. Somehow I expected a compiler error.
r
Well there’s no compiler error as the code is correct
The cast never succeeding isn’t an error, just probably not something you wanted to do, that’s a warning
s
Well, it would be quite opinionated to error on it but it would be reasonable in my opinion. ie. java errors on unreacheable code (Kotlin doesnt so is similar to this case)
r
You can set that warning as being an error in your IDE if you want.
j
In our codebase we'd prefer to treat this as an error since it means that the reasoning of the code is wrong.
Unfortunately the IDE is not our quality gatekeeper, CI is...
j
Interesting, I hadn't found that. I wonder how hard it would be to trigger that from gradle.
r
j
Ha! Ask and thou shallt receive