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

Self-defined Classes in one cell cannot be invoked in another cell #35

Closed
TaoRuan opened this issue May 18, 2017 · 10 comments
Closed

Self-defined Classes in one cell cannot be invoked in another cell #35

TaoRuan opened this issue May 18, 2017 · 10 comments

Comments

@TaoRuan
Copy link

TaoRuan commented May 18, 2017

I am trying to run some code with self defined classes in Scala. But when I try to use them in another cell, it said reference to List is ambiguous.

@TaoRuan TaoRuan closed this as completed May 18, 2017
@parente
Copy link
Contributor

parente commented May 19, 2017

I have a PR pending with a fix for certain uses of case classes in Spark executor run functions. I'm not sure if this covers the problem you're seeing. Can you provide an example of the code and a stack trace? Or did you mean to close this issue as you found a fix?

@TaoRuan
Copy link
Author

TaoRuan commented May 19, 2017

@parente
Hello! Thanks so much for asking. Actually I did not find a fix. I closed the issue simply because I did not find a good way to upload my code. They look so ugly. Wait a minute please

@TaoRuan
Copy link
Author

TaoRuan commented May 19, 2017

@parente

sealed trait List[+A]
case object Nil extends List[Nothing]
case class Cons[+A](head: A, tail: List[A]) extends List[A]
object List {
def sum(ints: List[Int]): Int = ints match {
case Nil => 0
case Cons(x,xs) => x + sum(xs)
}
def product(ds: List[Double]): Double = ds match {
case Nil => 1.0
case Cons(0.0, _) => 0.0
case Cons(x,xs) => x * product(xs)
}
def apply[A](as: A*): List[A] =
if (as.isEmpty) Nil
else Cons(as.head, apply(as.tail: _*))
}

def sum(ints: List[Int]): Int = ints match {
case Nil => 0
case Cons(x,xs) => x + sum(xs)
}

val x = List(1,2,3,4,5) match {
case Cons(x, Cons(2, Cons(4, _))) => x
case Nil => 42
case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y
case Cons(h, t) => h + sum(t)
case _ => 101
}

@TaoRuan
Copy link
Author

TaoRuan commented May 19, 2017

Above is all the coding I put into one cell and it worked well with the outputs:

defined trait List
defined object Nil
defined class Cons
defined object List
sum: (ints: List[Int])Int
x: Int = 3

@TaoRuan
Copy link
Author

TaoRuan commented May 19, 2017

However when I put exactly the same last part of the code in the next cell, it cannot work

@TaoRuan
Copy link
Author

TaoRuan commented May 19, 2017

val x = List(1,2,3,4,5) match {
case Cons(x, Cons(2, Cons(4, _))) => x
case Nil => 42
case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y
case Cons(h, t) => h + sum(t)
case _ => 101
}


Output:
<console>:40: error: reference to List is ambiguous;
it is imported twice in the same scope by
import $line16$read.List
and import INSTANCE.List
       val x = List(1,2,3,4,5) match {
               ^
<console>:43: error: type mismatch;
 found   : Any
 required: String
       case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y
                                                         ^
<console>:44: error: type mismatch;
 found   : List[Any]
 required: List[Int]
       case Cons(h, t) => h + sum(t)
                                  ^

@TaoRuan
Copy link
Author

TaoRuan commented May 19, 2017

Updated the code. Please have a look if possible

@mariusvniekerk
Copy link
Collaborator

did you redefine List? Pretty sure that the scala repl is going to get somewhat angry at that since as part of the repl everything in scala.Predef gets pulled into the class that gets built for evaluating a cell.

@TaoRuan
Copy link
Author

TaoRuan commented May 19, 2017

@mariusvniekerk Thanks! That should be the problem.

@TaoRuan
Copy link
Author

TaoRuan commented May 19, 2017

@mariusvniekerk I tried to run the code in the original Scala REPL but I need not redefine the class every time. Whenever I run the last part of code it can work smoothly. I had expected the same thing in Jupyter Notebook but it seemed to be hard

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

3 participants