https://kotlinlang.org logo
#announcements
Title
# announcements
i

Ifvwm

07/18/2019, 5:04 AM
this code is converted by intellij idea, I don't understand why it tonvert a f(a,b) in java to f(a)(b) in kotlin, auto currying? https://paste.ubuntu.com/p/NNbRwmR4mG/
also why intellij idea turn a object to lambda in memory.subscribeToEvent( "FrontTactilTouched", new EventCallback<Float>() {})
t

Toddobryan

07/18/2019, 5:14 AM
Two things are going on...The first is that as of Java 8, interfaces with exactly one function (known as "functional interfaces") can be replaced by lambdas. My guess is that
EventCallback<T>
is actually an interface and the code is creating an anonymous class that implements it. Kotlin knows that you can just use a lambda instead and does.
The second thing is Kotlin syntactic sugar. Whenever you have a function of the form
func(..., f: (...) -> T)
(so
f
is a function type and is the last argument in `func`'s parameter list), you can move
f
outside the parentheses and include it as a block within curly braces after the close paren.
i

Ifvwm

07/18/2019, 5:17 AM
but the lambda is supposed in the memroy.substribeToEvent("FrontTactilTouched", a-lambda), but why kotlin turn it to memory.substribeToEvent("FrontTactilTouched"){a-lambda} ?
t

Toddobryan

07/18/2019, 5:18 AM
In other words, you call
func
like this:
Copy code
func(....) { (...) -> // these are arguments to f
    ...  // and this part better return a T
}
Because it can? You can also put it inside the parens. They're just different ways of writing the same thing.
i

Ifvwm

07/18/2019, 5:20 AM
ok, then how to do override a method when instantiate a class to an object?
t

Toddobryan

07/18/2019, 5:20 AM
Not sure what you mean here. Example?
i

Ifvwm

07/18/2019, 5:21 AM
memory.subscribeToEvent( "FrontTactilTouched", new EventCallback<Float>() { @Override public void onEvent(Float arg0) throws InterruptedException, CallError { // 1 means the sensor has been pressed if (arg0 > 0) { tts.say("ouch!"); } } });
the second parameter, is new EventCallback<Float>(){ @Override ...}
this will create an object, and this object's onEvent method is override, right?
which intellij idea turn it to a lambda
t

Toddobryan

07/18/2019, 5:25 AM
Yeah. In Java it's an instance of an anonymous class. If you're using Java 8, you could also write that as...
Copy code
memory.subscribeToEvent(
               "FrontTactilTouched", (Float arg0) -> if (arg0 > 0) tts.say("ouch!"));
I think. Let me know if that works.
i

Ifvwm

07/18/2019, 5:27 AM
ok, I will try it now
I tried this in kotlin
memory.subscribeToEvent( "FrontTactilTouched"){ (arg0:Float) -> if (arg0 > 0) println("ok") }
destructuring declaration initializer of type Any! must have a 'component1()' function
red line under arg0:Float
t

Toddobryan

07/18/2019, 5:32 AM
Try getting rid of the parens around those.
And put a space after the colon. (Shouldn't make a difference, but who knows.)
i

Ifvwm

07/18/2019, 5:33 AM
Type mismatch: inferred type is (Float) -> Unit but EventCallback<(raw)Any!>! was expected
I guess it may should be an object like that in java, not a lambda
t

Toddobryan

07/18/2019, 5:36 AM
Is this in Java or Kotlin?
i

Ifvwm

07/18/2019, 5:36 AM
that example is in java, and I'm trying to convert it to kotlin
that error is in kotlin
t

Toddobryan

07/18/2019, 5:38 AM
Which version of Java are you using?
i

Ifvwm

07/18/2019, 5:38 AM
jdk 8
t

Toddobryan

07/18/2019, 5:40 AM
In Kotlin, you could try
EventCallback<Float>() { override fun onEvent(arg0: Float): Unit = if (arg0 > 0) println("ouch!") }
So without the
new
and converting the class body to a Kotlin function.
i

Ifvwm

07/18/2019, 5:44 AM
memory.subscribeToEvent( "FrontTactilTouched", EventCallback<Float>() { override fun onEvent(arg0: Float): Unit = if (arg0 > 0) println("ouch!") else println("1")})
modifier 'override' is not applicable to 'local function'
t

Toddobryan

07/18/2019, 5:44 AM
So get rid of
override
?
i

Ifvwm

07/18/2019, 5:44 AM
that is ok?
t

Toddobryan

07/18/2019, 5:45 AM
Does it work?
i

Ifvwm

07/18/2019, 5:45 AM
@Override is an annotation in java, and override in kotlin is?
t

Toddobryan

07/18/2019, 5:45 AM
a keyword.
s

SiebelsTim

07/18/2019, 5:45 AM
The above looks wrong to me. This seems like a lambda containing a local function. Get rid of the fun etc.
t

Toddobryan

07/18/2019, 5:46 AM
And there are weird rules about where you need it. Maybe not when implementing an
interface
? I'm not sure.
No. That's how you instantiate a class in Kotlin.
You could try
Copy code
memory.subscribeToEvent(
       "FrontTactilTouched",
       arg0 -> if (arg0 > 0) println("ouch!") else println("1"))
and see if that works any better than the one with the type did.
s

SiebelsTim

07/18/2019, 5:48 AM
It's not. You either want object : EventCallback<BLA>{} or you want to omit the fun stuff.
t

Toddobryan

07/18/2019, 5:50 AM
You're right.
Copy code
memory.subscribeToEvent(
       "FrontTactilTouched",
       object: EventCallback<Float> {
           override fun onEvent(arg0: Float): Unit = if (arg0 > 0) println("ouch!") else println("1")})
Maybe.
i

Ifvwm

07/18/2019, 5:52 AM
I will try it
t

Toddobryan

07/18/2019, 5:55 AM
It looks like this should also work, since there's only one method in the interface:
Copy code
memory.subscribeToEvent(
       "FrontTactilTouched",
       EventCallback<Float> { if (it > 0) println("ouch!") else println("1") })
but these are the things about Kotlin that I haven't used much, yet.
s

SiebelsTim

07/18/2019, 5:55 AM
https://pl.kotl.in/dCf-EWIJe The error you see is due to the interface not coming from java. If the interface is coming from Java, you get SAM conversions.
t

Toddobryan

07/18/2019, 5:56 AM
Ah. So Kotlin handles Java interfaces better than Kotlin interfaces? Interesting.
Anyway, good luck. It's late here.
i

Ifvwm

07/18/2019, 5:56 AM
touch its head sensor, and nothing happen, it suppose to stop dialog function, and it doesn't
s

SiebelsTim

07/18/2019, 5:57 AM
Yeah, SAM for kotlin interface/classes is wanted for quite some time https://youtrack.jetbrains.com/issue/KT-7770
t

Toddobryan

07/18/2019, 5:57 AM
What's the error?
i

Ifvwm

07/18/2019, 5:57 AM
there's no error
I suppose the touch event don't work properly
s

SiebelsTim

07/18/2019, 5:58 AM
You should use the debugger to figure out the problem. You're past the point of fighting the compiler, so without us knowing your code behind the scenes we can't help.
t

Toddobryan

07/18/2019, 5:58 AM
Try just printing the value of the arg. That way you can touch it multiple times and see if anything happens.
i

Ifvwm

07/18/2019, 5:59 AM
ok,
t

Toddobryan

07/18/2019, 5:59 AM
Good luck!
i

Ifvwm

07/18/2019, 5:59 AM
good night
EventCallback<Float> { if (it > 0) println("ouch!") else println("1") } I don't understand the syntax, put a type before a lambda, is ?
it's like EventCallback<Float>() { a-lambda } ?