https://kotlinlang.org logo
#android
Title
# android
j

Jordan Carlyon

03/30/2020, 6:27 PM
Hello. I am having some trouble figuring out what is going on in this Android sample Specifically
sealed class Result<out R>
From the generics documentation
The general rule is: when a type parameter 
T
 of a class 
C
 is declared out, it may occur only in out-position in the members of 
C
, but in return 
C<Base>
 can safely be a supertype of 
C<Derived>
.
In “clever words” they say that the class 
C
 is covariant in the parameter 
T
, or that 
T
 is a covariant type parameter. You can think of C as being a producer of `T`'s, and NOT a consumer of `T`’s.
So I can see that type parameter
R
of class
Result
can only be returned by
Result
,
Error
and
toString
but what is
R
? Is my assumption correct? I can see here that it is a callable reference
References to functions, properties, and constructors, apart from introspecting the program structure, can also be called or used as instances of function types.
The common supertype for all callable references is 
KCallable<out R>
, where 
R
 is the return value type, which is the property type for properties, and the constructed type for constructors.
But I am not sure what that is or why it matters here. Is this applicable to this sealed class or is this not considered a callable reference since it is a sealed class? Is the constructor the callable reference and then the return type is
Change<R>
?
i

itnoles

03/30/2020, 6:40 PM
ViewState 🙂
you supposed to call like Result.Success()
j

Jordan Carlyon

03/31/2020, 1:19 AM
Can you clarify what you mean? Thanks!