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?

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.