https://kotlinlang.org logo
Title
p

Paulius Ruminas

06/04/2019, 6:29 PM
Hi, TypeScript uses the same convention to describe constraints for generic types for both classes and functions e.g.
function foo<T extends Bar>() {}
and
class Foo<T extends Bar> {}
. Just out of curiosity were there any limitations why it couldn't be like
fun foo<T : Bar>() {}
in Kotlin?
🤔 2
r

Ruckus

06/04/2019, 6:32 PM
fun <T : Bar> foo<T>() {}
isn't valid Kotlin. Did you mean:
fun <T : Bar> foo() {}
p

Paulius Ruminas

06/04/2019, 6:32 PM
Yes
Classes are described like
class Foo<T : Bar>
so I'm wondering why functions do not follow the same convention.
r

Ruckus

06/04/2019, 6:34 PM
I don't think your suggestion is any more "natural" looking. It may be more familiar to you, but not intrinsically natural. The seeming disagreement between class and function definitions is a much more valid question.
(Also, your "needs less typing argument" doesn't make sense, as it's the exact same thing in a different spot)
p

Paulius Ruminas

06/04/2019, 6:39 PM
Yes the main reason I'm asking is why they do not follow the same convention as classes or vice versa.
r

Ruckus

06/04/2019, 6:46 PM
With your edits, I now look like a crazy guy complaining about stuff that isn't there 😆
p

Paulius Ruminas

06/04/2019, 6:47 PM
I wanted to make it more clear as you pointed out a lot of mistakes in the question. Sorry 😄
r

Ruckus

06/04/2019, 6:48 PM
I only meant it looks funny reading through it now. The edits were definitely good and hopefully get you a good answer.
d

Dico

06/04/2019, 11:43 PM
I was wondering this some time back, forgot what the answer was..
k

karelpeeters

06/05/2019, 11:45 AM
I remember reading a Kotlin discussions thread about this a while ago, and I think the explanation was the receiver type:
fun T.foo<T>()
is weird. The same reason works for Java with the return type:
public T foo<T>()
would be weird.
🤔 1
1
"weird" as in "a type is used before it is declared"
p

Paulius Ruminas

06/05/2019, 12:23 PM
I think the explanation was the receiver type:
fun T.foo<T>()
is weird.
probably this is why they chose the current syntax.