tags:

views:

96

answers:

2

I'm using the Scalacheck library to test my application. In that library there's a Gen object that defines implicit conversions of any object to a generator of objects of that class.

E.g., importing Gen._ lets you call methods such as sample on any object, through its implicit conversion to Gen:

scala> import org.scalacheck.Gen._
import org.scalacheck.Gen._

scala> "foo" sample
res1: Option[java.lang.String] = Some(foo)

In this example, the implicit Gen.value() is applied to "foo", yielding a generator that always returns Some(foo).

But this doesn't work:

scala> import org.scalacheck.Gen.value
import org.scalacheck.Gen.value

scala> "foo" sample                   
<console>:5: error: value sample is not a member of java.lang.String
       "foo" sample
             ^

Why not?

Update

I'm using Scala 2.7.7final and ScalaCheck 2.7.7-1.6.

Update

Just switched to Scala 2.8.0.final with ScalaCheck 2.8.0-1.7. The problem did indeed go away.

A: 

Simple: You did not import the implicit conversion (whatever its name is), you only imported something called value from object org.scalacheck.Gen.

Correction / clarification:

Gen.value (that's object Gen, not trait Gen[+T]) is the implicit used to wrap arbitrary values in an instance of (an anonymous class implementing) trait Gen[T] (where T is a function from Gen.Params to the argument to which Gen.value is applied). Gen.sample is a method of trait Gen[T] that invokes its (the concrete Gen subclass) apply method to get the synthesized value.

Sadly, having looked closer, I have to admit I don't understand why the code doesn't work when the rest of the members of object Gen are not imported.

Randall Schulz
+3  A: 

I just tried this with Scala 2.8.0.final and ScalaCheck 1.7 built for the same. Both imports worked, meaning the second line produced the desired result for both imports:

scala> "foo" sample
res1: Option[java.lang.String] = Some(foo)

What version of Scala and ScalaCheck did you use?

Dean Wampler
I'm using Scala 2.7.7final and ScalaCheck 2.7.7-1.6. Is this a bug in those versions? Should I upgrade?
lindelof
It might be 2.7.7 limitation, but upgrading to 2.8 is a big step, so do that only when you're ready.
Dean Wampler
@lindelof: 2.7 is a dead end and is not being maintained. I strongly advise upgrading to 2.8 ASAP. It is not as big a task as you might think.
Randall Schulz