Is it considered SAM conversion when you have some...
# getting-started
d
Is it considered SAM conversion when you have something like this?
Copy code
fun someFunction(thing: List<Int>) { /* ... */ }

fun someOtherFunction((List<Int>) -> Unit) { /* ... */ }

someOtherFunction(::someFunction)
e
d
Are you linking that to show what SAM conversion is?
If it’s not SAM conversion, is there a name for this?
“this” being the ability to pass a declared function by reference to a functional type parameter
d
I’m still not clear on if there is a name for this
::someFunction
is a value of a function type so my example would be passing a function reference by value?
e
"by value" is misleading IMO since it's not a primitive type so the function reference is being passed by reference (or a reference to the reference is being passed by value, however you want to look at it)
that's just true of everything in Kotlin/JVM
you're using a function reference as an instance of a function type
d
Would it be fair to call this functional polymorphism?
e
no, how is that related
d
Well, it’s sort of like polymorphism in OOP, no?
e
no
d
You have a type, in this case a functional type, and you can pass in an “implementation” of that type which may behave differently than other “implementations”
e
that doesn't make it polymorphism
d
but ultimately they have the same inputs/outputs
e
::someFunction
is just a shortcut for
{ someFunction(it) }
in this context
d
yeah, I’m not talking about specifically using a function reference
e
you could consider it polymorphism if there were different types involved, but there is nothing in the docs saying that function references and lambdas have different types
d
but rather the ability to pass many different functions to the same “interface”
That’s why I called it “functional polymorphism”
because it’s not actual polymorphism
as in the OOP term
the functionality seems pretty similar though
a single interface that can be implemented in multiple different ways
e
if we're talking about functional programming then polymorphism also refers to types (although it generally means generalizing over types)
d
Actually, since the function reference is of one of the KFunction types, wouldn’t it effectively be an anonymous implementation of that particular KFunction type?
which would make it just regular ol’ polymorphism
e
all callable references could be implemented by a single KFunction_N_ type that just uses reflection (they aren't, but they could be)
there's nothing proscribing polymorphism here
d
🤔