1

I've attempted to implement an implicit string conversion, as an experiment with creating scala object "factories", for example, in this case, I'd like to create an employee object from a string.

implicit def s2act(name: String) = new Actor[Employee](){
  override def toString() : String = {
    name
  }
};
//two failed attempts at leveraging the implicit conversion below...
processActor("jim")
def boss : Actor = new String("Jim");

However, the above implicit binding fails to be identified by the compiler. Why does the compiler fail to process this implicit binding?

jayunit100
  • 17,388
  • 22
  • 92
  • 167

2 Answers2

2

It seems to me that the error you should get from your code is that "class Actor takes type parameters".

a more correct code will be :

def boss : Actor[Employee] = new String("Jim");
faissalb
  • 1,739
  • 1
  • 12
  • 14
1

I don't know what's the signature of processActor, but judging from the signature of boss I think the problem is you don't provide type parameter to Actor.

Actor itself is a type constructor, which means that it takes a type to construct a new type.

Actor is not a type, and you cannot use it in your function/method signature, but Actor[Employee] is a type.

Change your boss to this

def boss: Actor[Employee] = new String("Jim")

Related post: What is a higher kinded type in Scala?

Community
  • 1
  • 1
Herrington Darkholme
  • 5,979
  • 1
  • 27
  • 43