On Sun, Jul 5, 2009 at 2:58 PM, Paul Phillips
<paulp@...> wrote:
On Sun, Jul 05, 2009 at 01:11:03PM +0200, Viktor Klang wrote:
> Given the following code, the 2.7.5 Scalac compiler tosses me a fine
> error: "constructor Name cannot be accessed in object ValueTypes1"
Companion objects don't really work if they're not top level.
Ah, ok, I just put it in an object to scope it...
> Now, since I cannot write: *Some( new T(f) ) *in the apply-method of
> the TypeDefFactory, I see no other solution than modify TypeDefFactory
> so the inheriting class/object has to define a "create" method as so:
class Name private[ValueTypes1] (val value : String)
> Why is "new Name(_)" in the ValueTypes1 scope instead of the Name
> scope?
This is not a scoping issue, but a limitation of access modifiers.
"new Name(_)" means what you want it to mean, it's just locked out.
I'm not sure I follow _why_.
Given the following code below:
class Name private (val value : String) extends TypeDef[String,Name]
object Name extends TypeDefFactory[String,Name]( new Name(_), !_.isEmpty ) // new Name(_) causes the error
The call to TypeDefFactory's constructor with signature ((String) => Name, (String) => boolean) should occur within the Name scope, and since Name is a companion object of class Name, the constructor call should be visible to the companion object, which it is, outside the companion objects constructor.
object Name { def create = new Name("foo") } //This is valid..
So I fail to see why the class Name construtor is illegal in the constructor, but not in the companion body.
Could you direct me to the appropriate part of the SLS that explains this?
--
Paul Phillips | Beware of bugs in the above code; I have only
Everyman | proved it correct, not tried it.
Empiricist | -- Knuth
i'll ship a pulp |----------* http://www.improving.org/paulp/ *----------
--
Viktor Klang
Scala Loudmouth