error: polymorphic expression with default arguments

Posted by 0__ on Stack Overflow See other posts from Stack Overflow or by 0__
Published on 2012-10-28T14:26:25Z Indexed on 2012/10/28 23:02 UTC
Read the original article Hit count: 193

This following bugs me:

trait Foo[ A ]
class Bar[ A ]( set: Set[ Foo[ A ]] = Set.empty )

This yields

<console>:8: error: polymorphic expression cannot be instantiated to expected type;
 found   : [A]scala.collection.immutable.Set[A]
 required: Set[Foo[?]]
       class Bar[ A ]( set: Set[ Foo[ A ]] = Set.empty )
                                                 ^

It is quite annoying that I have to repeat the type parameter in Set.empty. Why does the type inference fail with this default argument? The following works:

class Bar[ A ]( set: Set[ Foo[ A ]] = { Set.empty: Set[ Foo[ A ]]})

Please note that this has nothing to do with Set in particular:

case class Hallo[ A ]()
class Bar[ A ]( hallo: Hallo[ A ] = Hallo.apply )  // nope

Strangely not only this works:

class Bar[ A ]( hallo: Hallo[ A ] = Hallo.apply[ A ])

...but also this:

class Bar[ A ]( hallo: Hallo[ A ] = Hallo() )      // ???

© Stack Overflow or respective owner

Related posts about scala

Related posts about default-value