0

In this simple example, I create a create a container class over a generic type:

class  Student[T](val favoriteThing:T, var partner:Option[Student[T]]=None )

The first field favoriteThing is of generic type T and the second field is a pointer to another instance of the class, but in an Option type, and it has a default value of None.

I can instantiate this class and examine the first field

val s1 = new Student(42)
s1.favoriteThing

But get this runtime error when accessing the partner field.

s1.partner
Compiler exception error: line 0: can't existentially abstract over parameterized type Student[Int]
  def apply() = {
            ^

Appreciate any help in understanding this error.

keegan
  • 2,892
  • 1
  • 17
  • 20
  • 2
    Something seems to be missing as the code above runs smoothly for me. What scala version are you using? – Sascha Kolberg Apr 02 '16 at 12:03
  • You probably have an unbound *Type Constructor* in a *block*. LIke `{class A[B] {}; new A[Int]}`. For more info: http://stackoverflow.com/questions/3122398/cant-existentially-abstract-over-parameterized-type?rq=1 – Herrington Darkholme Apr 02 '16 at 13:29
  • Please post more relevant code and what you have posted works fine. – Som Bhattacharyya Apr 02 '16 at 14:01
  • @SaschaKolberg Same behaviour for me. It just works. – tuxdna Apr 02 '16 at 15:03
  • thanks for the feedback - I had been testing with scalafiddle http://scalafiddle.net/console/e25f42d74afdf8c6ca6a3b7ece3af051 And that seems to be the source of the problem. Will be more cautious with scalafiddle in the future... – keegan Apr 02 '16 at 15:05

1 Answers1

0

This was not a problem with scala. The issue was in using scalafiddle

http://scalafiddle.net/console/e25f42d74afdf8c6ca6a3b7ece3af051

This compiles and runs properly outside of that context.

keegan
  • 2,892
  • 1
  • 17
  • 20