SørenHN
SørenHN

Reputation: 696

Type aliasing of Lists results in same type after erasure

When using type aliasing to define lists of two different traits as their own type, I am hit with the error message: error: double definition: func and def func have same type after erasure.

I have read about type erasures, but can't seem to come up with a solution. So, how can I circumvent this issue?


This is an example of what I am trying to do.

sealed trait A
sealed trait B

type AList = List[A]
type BList = List[B]

def func(l: AList): AList = l
def func(l: BList): BList = l

Upvotes: 1

Views: 47

Answers (1)

Mario Galic
Mario Galic

Reputation: 48430

Try disambiguating with DummyImplicit like so

def func(l: AList): AList = l
def func(l: BList)(implicit ev: DummyImplicit): BList = l

val aList = List(new A {})
val bList = List(new B {})

func(aList)  // ok
func(bList)  // ok

This works because compiler -Xprint:jvm translates it to something like

def func(l: List): List = l;
def func(l: List, ev: DummyImplicit): List = l;

func(aList);
func(bList, scala.DummyImplicit.dummyImplicit);

Upvotes: 3

Related Questions