Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxScala: Subscribe with anonymous case matching #1411

Closed
jbripley opened this issue Jul 5, 2014 · 3 comments
Closed

RxScala: Subscribe with anonymous case matching #1411

jbripley opened this issue Jul 5, 2014 · 3 comments

Comments

@jbripley
Copy link
Contributor

jbripley commented Jul 5, 2014

This is somewhat related to #1398, with all the changes to the Observable method signatures to improve type inference. I was hoping something could be done to how subscribe(onNext: T => Unit): Subscription is defined, so the common Scala simplification of case class matching from example 1 to example 2 would still compile.

Example 1

import rx.lang.scala.Observable

object Subscribe extends App {
  Observable.items(Foo("Answer", 42)).subscribe({ foo =>
    foo match {
      case Foo(bar, boo) => println("Got Foo")
      case _ => println("Got Other")
    }
  })
}

case class Foo(bar: String, boo: Long)

Example 2

import rx.lang.scala.Observable

object Subscribe extends App {
  Observable.items(Foo("Answer", 42)).subscribe({
    case Foo(bar, boo) => println("Got Foo")
    case _ => println("Got Other")
  })
}

case class Foo(bar: String, boo: Long)

From my limited understanding of Scala type inference, the problem is that the compiler can't differentiate between subscribe(onNext: T => Unit): Subscription, subscribe(subscriber: Subscriber[T]): Subscription or subscribe(observer: Observer[T]): Subscription. Which leads to the dreaded "missing parameter type for expanded function. The argument types of an anonymous function must be fully known (SLS 8.5)" error.

If someone can point me in the right direction, I could create a pull request implementing the change and update examples, test cases etc as required. If this is simply not possible with Scala current type inference support, just close this issue and I'll have to live with the more verbose case class matching syntax.

/cc @headinthebox, @zsxwing, @samuelgruetter for feedback.

@samuelgruetter
Copy link
Contributor

The simplest solution to make your Example 2 work is to replace subscribe by foreach, which is just an alias for subscribe, but has no overloads of the same arity, so the problem that the compiler can't differentiate between the different overloads is gone.

@jbripley
Copy link
Contributor Author

jbripley commented Jul 5, 2014

Thanks, didn't think of using foreach, but that looks like a simple enough workaround.

Haven't used foreach much, since I don't really like that it hides the subscribe, making the code harder to read and understand IMHO, but I guess I'll have to make a choice.

@jbripley jbripley closed this as completed Jul 5, 2014
@samuelgruetter
Copy link
Contributor

Originally I didn't like foreach either, but I was convinced to like it at least a bit ;-) See #1316 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants