This message was deleted.
# getting-started
s
This message was deleted.
y
I believe it's function calls that trigger SAM conversion You can also do it explicitly:
Copy code
fun map(f: () -> Unit): F = F(f)
The plot thickens:
Copy code
fun main() {
    val f: () -> Unit = {}
    useF(f)
}
fun useF(f: F) {}
fun map(f: () -> Unit): F { return { f() } }
I think this means lambda literals can always be SAM converted, and function arguments can as well. In your case, you have a non-literal and it's not a function argument, hence why it doesn't work
Likely due to generics if I had to guess
m
I won't be able to give you a definite comprehensive 100% accurate rule which works for all of the cases, as there are multiple different moving parts in most of your examples, and I don't have enough time to go in-depth enough on all of them. The simplest model which works often enough is kinda as follows. 1. Lambdas (and anonymous functions) are inferred to functional types (e.g.,
() -> Unit
). 2. For the purposes of type inference, we often treat a fun interface type as its corresponding functional type (e.g.,
MyContract
=>
() -> Unit
). 3. In some cases, we allow to put a square peg into a round hole, aka to put
() -> Unit
to
MyContract
, that is called SAM conversion. 4. When SAM conversion is and isn't allowed is messy. a. As @Youssef Shoaib [MOD] mentioned, it works for argument positions. b. It also works for some positions with explicit expected type (e.g., initializers):
val a: MyContract = {}
i. But! It sometimes works only for lambdas and not for arbitrary expressions. c. It does not work for callable reference inference. d. It does not work "inside" type inference. 5. The rabbit hole continues in KT-84394. My question to you @min would be -- what exactly are you trying to achieve?
If by an overload you mean a "ctor-like" function with the same name (aka
fun MyContract(impl: () -> Unit): MyContract
), then basically yes. And yes, we do not actually generate them to the binary, they are generated at the call-sites (similarly to other SAM conversions).
It's phrased like this, because your question is ambiguous 😃 "Is there an overload?" question implies there is already a callable, and you're asking whether we have another one with a different set of parameters. Which does not really apply here, as there is no "first" callable to overload in this case. Or I'm completely missing what you call an overload, which is also possible, do correct me if that's the case 🤝
y
I think it's technically actually a
(F) -> T
, where F is the function type that converts to this lambda. This means you can't do
T(T {})
, which is probably for the better
Copy code
fun main() {
    T { } // works
    T(T { }) // Argument type mismatch: actual type is 'T', but '() -> Unit' was expected.
}
fun interface T {
    fun t()
}
👌 1
thank you color 1
m
That's a bug in the spec, @Youssef Shoaib [MOD] is correct
Now it's known, and we have a backlog'd tracking issue for improving the specification w.r.t. conversions, but we don't have plans to do it in the near future.
What is the point of calling a
(T) -> T
function on a value of type
T
with the intent to get that exact value back?
At the moment, the intended behavior is as
<@U019AG342E5>
described. And in general, the rule of thumb is "if in doubt, the compiler is usually right". Despite Kotlin having a specification, it is neither complete, nor sound, and the single main source of truth is the compiler behavior. Your idea does make sense, and I quite like it, but it also makes code like
MyContract(myContract)
possible, which is (arguably) easy to write by mistake and which should be preferably avoided.