13

I'm confused by Kotlin lambda syntax.

At first, I have

.subscribe(
          { println(it) }
          , { println(it.message) }
          , { println("completed") }
      )

which works fine.

Then I moved the onNext to another class called GroupRecyclerViewAdapter which implements Action1<ArrayList<Group>>.

.subscribe(
          view.adapter as GroupRecyclerViewAdapter
          , { println(it.message) }
          , { println("completed") }
      )

However, I got the error:

error

Error:(42, 17) Type mismatch: inferred type is () -> ??? but rx.functions.Action1<kotlin.Throwable!>! was expected
Error:(42, 27) Unresolved reference: it
Error:(43, 17) Type mismatch: inferred type is () -> kotlin.Unit but rx.functions.Action0! was expected

I can fix the error by changing to:

.subscribe(
          view.adapter as GroupRecyclerViewAdapter
          , Action1<kotlin.Throwable> { println(it.message) }
          , Action0 { println("completed") }
      )

Is there a way to write the lambda without specifying a type? (Action1<kotlin.Throwable>, Action0)

Note: subscribe is RxJava method

Edit 1

class GroupRecyclerViewAdapter(private val groups: MutableList<Group>,
                           private val listener: OnListFragmentInteractionListener?) :
RecyclerView.Adapter<GroupRecyclerViewAdapter.ViewHolder>(), Action1<ArrayList<Group>> {
3
  • looks like a type reference error Commented Jan 23, 2016 at 21:41
  • 2
    its nice if you can include the error message as text, will help others find this post. You can compile, to get the same error you saw in the editor. Then cut-n-paste Commented Jan 24, 2016 at 22:23
  • @JaysonMinard added. thanks Commented Jan 24, 2016 at 22:35

2 Answers 2

12

view.adapter as GroupRecyclerViewAdapter part should be lambda func, not Action, since onError and onComplete also lambdas

so, to fix this try:

.subscribe(
          { (view.adapter as GroupRecyclerViewAdapter).call(it) }
          , { println(it.message) }
          , { println("completed") }
      )

with your names (replace Unit with your type)

class GroupRecyclerViewAdapter : Action1<Unit> {
    override fun call(t: Unit?) {
        print ("onNext")
    }
}

with lambdas

val ga = GroupRecyclerViewAdapter()
...subscribe(
    { result -> ga.call(result) },
    { error -> print ("error $error") },
    { print ("completed") })

with actions

...subscribe(
    ga,
    Action1{ error -> print ("error $error") },
    Action0{ print ("completed") })

pick one

Sign up to request clarification or add additional context in comments.

22 Comments

Nope, doesn't work. if it's the case, why my last snippet works then?
because in last example they all Actions. give me a sec i'll try
please provide view.adapter signature at least. is it really Action1? if it is in my example it should be (view.adapter as GroupRecyclerViewAdapter).call(it), sorry
Sure. added it. it's Action1
Hey guys, did it work or didn't? And where the rule of not using SAM with lambdas has come from? Never heard of that.
|
2

You have two versions of the subscribe method to choose from:

  • The first (the real one) has the signature subscribe(Action1<ArrayList<Group>>, Action1<Throwable>, Action0).
  • The second version is generated by the Kotlin compiler and has the signature subscribe((ArrayList<Group>>) -> Unit, (Throwable) -> Unit, () -> Unit)

In your code, however, you pass the following parameter types:

subscribe(
    view.adapter as GroupRecyclerViewAdapter, // Action1<Throwable>
    { println(it.message) },  // (Throwable) -> Unit
    { println("completed") } // () -> Unit
)

As you can see, these parameter types satisfy none of the available signatures. The other answer gives you some solutions to your problem. In addition, you can make GroupRecyclerViewAdapter implement the functional type Function1<ArrayList<Group>, Unit> (they're interfaces, too) instead of Action1<ArrayList<Group>>.

1 Comment

Would be nice to mix and match though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.