Is there a roadmap or plans for Kotlin to improve ...
# announcements
b
Is there a roadmap or plans for Kotlin to improve generics (breaking with Java)?
c
There is no simple answer for such a broad question. You could look at the issue tracker and if you can't find your specific problem, file an issue for it.
b
I do have specific generics features I’d like to see, but it seems like with reified types, there is some sense that there might be future plans to build more generics improvements. Maybe there is a KEEP or something like that?
n
reified generics are in the java roadmap because that is definitely a bytecode breaking change it will be much easier to wait for that to happen and only then implement it in kotlin
b
Ah so more reified generics coming then?
Do you know where I can find that roadmap by chance?
l
Kotlin 2.0 or later, if they ever do it. Reified generics is something that was experimented before Kotlin 1.0 but was dropped for performance reasons on the JVM.
beware, it’s an option
b
Thanks!
👍 1
g
@basher Just curious, what is your use case for reified generics that not covered by reified inline or upcoming
typeOf
operator?
As I understand the goal of Valhalla about generics is not real reified generics support, but specialised generics for primitive types, this is not exactly the same and will work only for generics for primitives and for classes that explicitly support it
👍 2
👆 1
b
@gildor i'm not familiar with
typeOf
actually. i'm looking to be able to call a constructor on a generic type that's constrained to inherit from an abstract class that requires that constructor
g
@basher wouldn't be more clear and flexible solution provide factory for the type? Because reified generics is huge thing that also doesn't solve all the cases (like what if you have interface, not an abstract class)
Also, there is related discussion about reified generics, especially check article about reified vs erased generics in second message of this thread https://kotlinlang.slack.com/archives/C0B9K7EP2/p1549657528197100?thread_ts=1549657528.197100&cid=C0B9K7EP2
b
wouldn't be more clear and flexible solution provide factory for the type?
Being frank: coming from another language where it's common to have static requirements on generic abstract types and interfaces, factories feel like a crutch. You instantiate them once, so that you can use their instance methods like static methods
g
But isn't it break encapsulation and make code less flexible? Also, how can you instantiate interface?
Also if you have instance of some interface/class, maybe better add factory to interface, so it would be encoded to types
b
As a side-effect, factories do make you think about how you want to break up your code, but this feels like overkill for something simple (see contrived example below).
Also, how can you instantiate interface?
Copy code
fun <T: SomeInterface> makeOne(): T {
    return T()
}
If interfaces allowed static requirements, like constructors, you could do something like that ^
g
But there is no proof (from type system) that such class has default constructor, so what should happen, crash on runtime? Looks too dynamic and unsafe for me. Also with inline function + reflections you can do this even now
Copy code
inline fun <reified T: SomeInterface> makeOne(): T {
    return T::class.java.newInstance()
}
b
Right that's the state today. I'm trying to make the case for
constructor
defs in `interface`s
g
But there is no such thing as constructor in interface
b
I'm wishing there was 🙂
I started this discussion asking about the roadmap for generics, hoping that something like this would be on it 🙂
g
But why? Isn't factory solves this in a more flexible way?
b
It solves it, and it's still a tool that you can use in both worlds. But that requires a non-trivial amount of extra code, compared to (for these kinds of small cases):
Copy code
fun <T: SomeInterface> makeOne(): T {
    return T()
}
g
Also, what is you real use case, because this is probably a part of some more complex feature where you need this
b
We implemented a generic sync system in Swift, which relies heavily on static requirements in interfaces (protocols in Swift). I've found that as I try to write the same thing in Kotlin, I spend a lot of time writing factories to work around this constraint
g
This doesn't look for me as non-trivial amount of code
Copy code
fun <T : SomeInterface> makeOne(factory: () -> T): T {
   return factory()
}
Also you get flexibility and compile time safety
In a simple case usage is also pretty concise: makeOne(::SomeImpl)
b
That's true. I agree it's not very hard to write a factory. But coming from a language without them. That contrived example is simpler without them though (no indireection through the lambda). In Swift, you still get compile time safety because the interface/protocol has the constructor requirement, so the compiler knows what you're calling.
g
I want to be clear, that there are valid use cases for reified generics for sure, but this is so huge feature with a lot problems that I just not sure that such use cases is really worth it
Isn't constructor in interfaces doesn't require reified generics and may work itself?
And reified generics without constructor doesn't give you compile type safety
b
I don't think I'll be able to convince you that it's worth it. All I can say is that I've found I've been able to write concise code with it without factories, which I think was nice.
Also reified generics without constructor doesn't give you compile type safety
I don't know enough about Kotlin/Java to refute that yet. Swift is able to do this with compile-time safety though somehow, but they're different languages with different constraints. 🤷‍♂️
g
No need to convince me, I really curious about use cases. There is a lot of nice features, but each language feature has a price and price of reified generics is pretty high imo
b
What is the price exactly?
g
Performance, language complexity, interopability with other platform languages (java, js, C), maintenance cost
🤷‍♂️ 1
And any kinds of problems with feature itself (how it works with other language features for example)
b
TBH, those feel like arguments to not build any new language features
g
Yes, sure, it is, but it's valid argument, otherwise you will get Scala or C++
b
Those are both pretty popular, well-liked languages
g
And very complex with own problems that harm them a lot
b
Doesn't mean Kotlin needs to converge to be like those, but it's certainly an opportunity to become something great, which means making useful features
g
"Interesting" feature is probably not really a goal of Kotlin, as I understand it
b
Updated 🙂
g
Yes, "useful" definitely make sense, also "practical"
It's more philosophical thing, instead I would really like to discuss use cases and examples of real code where it would be helpful and discuss problems that it may cause
b
I presented one that is both practical and well used in our codebase (also well-used in the Swift language/stdlib), and you responded with "here's the Java pattern that solves it." It doesn't seem like you're open to that discussion, if there's already a code pattern that solves the use case. With that same argument, you don't need generics either
g
This is not "the java pattern", it's universal pattern that also may be used even with reified generics and interface constructors if you need more flexible solution
If you have a hammer everything looks as a nail, so if some feature used a lot in one language it doesn't mean that you don't have alternatives in another language
Like macroses used a lot in languages where they exist, but it doesn't mean that it good feature just because of that
b
So now it doesn't matter what use cases I present?
g
Why do you think so? I just pointed that reified generics itself cannot solve proposed use case in a safe way and that existing alternatives are not so bad, that's all, this is just my opinion, sorry if something in my messages looked rude for you
c
Sounds like a nifty feature, which has many drawbacks within Kotlin ideology (pragmatic language with near-100% near-seamless Java interop). If you think that its pros outweigh cons, I'd suggest writing a KEEP, maybe it will catch on. This discussion doesn't seem very productive at this point 🙂
b
@Czar good point i'll consider that thanks! anything you can point me to about how to write one (tips or just a well-written one)?
g
Check Kotlin KEEP repo Readme and other proposals in PR section
c
_ _👆
b
thanks!