c# - Any clean way for generic parent to get type of child?
-
@Zecc Based on this, I propose the keywords be changed to the following
fun
for a functionsad
for constants/single static assignment valuesind
for variables
-
@cvi said in c# - Any clean way for generic parent to get type of child?:
@Zecc Based on this, I propose the keywords be changed to the following
fun
for a functionsad
for constants/single static assignment valuesind
for variables
We can fork the language and call it BiPOLAR.
-
@cvi said in c# - Any clean way for generic parent to get type of child?:
@Zecc Based on this, I propose the keywords be changed to the following
fun
for a functionsad
for constants/single static assignment valuesind
for variables
That means we can have
‼fun‼
for functors!
-
class Foo operator fun Foo?.not(): Foo? = this fun main() { val `fun`: Foo? = Foo() !!`fun`!! }
-
@pie_flavor doesn't
!!
have higher precedence than!
?
-
@Gąska does it matter?
-
@pie_flavor I'd think so. Why is a method defined on
Foo?
called onFoo
?
-
@Gąska
toString
is defined onAny?
but is still called on normal objects.
-
@pie_flavor what
toString
?Edit: are you trying to say that every non-nullable type in Kotlin is automatically a subtype of its respective nullable type?
-
@Gąska Yes.
-
@pie_flavor is this preserved by generics? If so, are other kinds of subtyping preserved by generics?
-
@Gąska this is Java. Nothing is preserved in generics. Unless you mean "can
List<T>
be assigned toList<T?>
, in which case yes.
-
@pie_flavor so generics are covariant. Fucking great. I guess nothing is perfect... Is there some kind of opt-out at least? And please tell me regular inheritance doesn't cause covariance.
-
@Gąska what are you on about?
interface List<out E> : Collection<E>
As opposed toMutableList<>
which is invariant.
-
@Gąska said in c# - Any clean way for generic parent to get type of child?:
generics are covariant
In Java (with no guarantee about other JVM-based languages), you get to select whether generics are invariant (usual case), covariant or contravariant. (I can never remember which way round
extends
andsuper
map to those two terms.)
-
@pie_flavor said in c# - Any clean way for generic parent to get type of child?:
@Gąska what are you on about?
The same thing as you, except in more words because I'm not a fucking asshole, and more questions than answers because I'm not a fucking expert on Kotlin unlike you.
interface List<out E> : Collection<E>
As opposed toMutableList<>
which is invariant.Okay, so
out
specifies covariance, and if you omit it, it's invariant, and you can set contravariance for a type parameter too. Dunno why you menthionedCollection
though, because ifA: B
, thenMutableList<A>: Collection<B>
too.
-
@Gąska I didn't 'mention' Collection. That's just the definition of List.
-
@Gąska said in c# - Any clean way for generic parent to get type of child?:
The same thing as you, except in more words because I'm not a fucking asshole, and more questions than answers because I'm not a fucking expert on Kotlin unlike you.