this code is converted by intellij idea, I don't u...
# announcements
i
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
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
but the lambda is supposed in the memroy.substribeToEvent("FrontTactilTouched", a-lambda), but why kotlin turn it to memory.substribeToEvent("FrontTactilTouched"){a-lambda} ?
t
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
ok, then how to do override a method when instantiate a class to an object?
t
Not sure what you mean here. Example?
i
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
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
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
Try getting rid of the parens around those.
And put a space after the colon. (Shouldn't make a difference, but who knows.)
i
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
Is this in Java or Kotlin?
i
that example is in java, and I'm trying to convert it to kotlin
that error is in kotlin
t
Which version of Java are you using?
i
jdk 8
t
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
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
So get rid of
override
?
i
that is ok?
t
Does it work?
i
@Override is an annotation in java, and override in kotlin is?
t
a keyword.
s
The above looks wrong to me. This seems like a lambda containing a local function. Get rid of the fun etc.
t
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
It's not. You either want object : EventCallback<BLA>{} or you want to omit the fun stuff.
t
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
I will try it
t
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
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
Ah. So Kotlin handles Java interfaces better than Kotlin interfaces? Interesting.
Anyway, good luck. It's late here.
i
touch its head sensor, and nothing happen, it suppose to stop dialog function, and it doesn't
s
Yeah, SAM for kotlin interface/classes is wanted for quite some time https://youtrack.jetbrains.com/issue/KT-7770
t
What's the error?
i
there's no error
I suppose the touch event don't work properly
s
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
Try just printing the value of the arg. That way you can touch it multiple times and see if anything happens.
i
ok,
t
Good luck!
i
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 } ?