This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Implicits:I must be missing something really obvious

4 replies
Kenneth McDonald
Joined: 2009-01-11,
User offline. Last seen 42 years 45 weeks ago.

In the following, "Lit" is a case class. I have no idea why the
compiler is not accepting this definition, any help would be most
appreciated.

Error message
--------------------
/Users/Ken/NetBeansProjects/ScalaApplication1/src/scalaapplication1/
rex.scala:178: error: expected start of definition
implicit def stringToLit(s:String) = Lit(s)
^
---------------------

In a monospaced font, the "^" points to the "d" in def.

David Hall
Joined: 2008-12-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Implicits:I must be missing something really obvious

Please give us more context?

Can we see the 10-15 lines each way?

My guess would be that you're defining an implicit at top level, and
it has to be within an object or class.

Kenneth McDonald
Joined: 2009-01-11,
User offline. Last seen 42 years 45 weeks ago.
Re: Implicits:I must be missing something really obvious

Yes, you're right--it wasn't at top level. I only skimmed the chapter
on implicits, and they looked so simple :-)

Now I have to go figure out how to put it in an object or class and
make it available through the rest of the
same file. As I mentioned before about this library, I'm writing it as
a learning experience :-)

Thanks,
Ken

On Jan 27, 2009, at 2:27 PM, David Hall wrote:

> Please give us more context?
>
> Can we see the 10-15 lines each way?
>
> My guess would be that you're defining an implicit at top level, and
> it has to be within an object or class.
>

David Hall
Joined: 2008-12-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Implicits:I must be missing something really obvious

object MyImplicits {
implicit def stringToLit(s:String) = Lit(s)
}

import MyImplicits._;

Kenneth McDonald
Joined: 2009-01-11,
User offline. Last seen 42 years 45 weeks ago.
Re: Implicits:I must be missing something really obvious

Thanks! I'd tried several similar things, but hadn't hit on the magic
combo--I was doing import rex. ..., forgetting about import nesting.

Cheers,
Ken

On Jan 27, 2009, at 5:23 PM, David Hall wrote:

> object MyImplicits {
> implicit def stringToLit(s:String) = Lit(s)
> }
>
> import MyImplicits._;
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland