Issue
IntelliJ is showing me context hints that my variables are of type (String..String?)
. I cannot find any mention of it on the internet, what is this type?
Solution
(String..String?)
represents a flexible type with lower bound String
and upperbound String?
(nullable string). This is not valid Kotlin code (it's not denotable) but it is used in the compiler internals and thus in IntelliJ's hints sometimes.
(On the JVM we often see platform types using !
as in String!
, which are a more specific case of flexible types)
It's Kotlin's way of saying it doesn't know whether the String
type declared for payload.email
is nullable or not (for instance if this is declared in Java, which doesn't distinguish those), and yet it doesn't want to enforce either of those, for convenience (hence "flexible").
As the name suggests, flexible types are flexible — a value of type (L..U) can be used in any context, where one of the possible types between L and U is needed
This means that even though the actual type of the value is "somewhere between String
and String?
", values of this type can be used even in places expecting String
, even though the real type of that value may be String?
and thus the value could be null.
This is useful because assuming it is String
would mean that null checks would be marked as redundant, and assuming it is String?
would force the developer to write null checks everywhere, even though they might know that this particular Java method cannot return null.
In general, it's a good practice to explicitly declare the type of a variable that you get from Java, to avoid the propagation of the platform type and the uncertainty (and unsafety) that comes with it:
val email: String = payload.email // if you know it cannot be null
val email: String? = payload.email // if you don't know
Answered By - Joffrey Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.