- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
simple language question
Thu, 2011-04-14, 13:21
Hello,
suppose I have the following library code
package com.imaginej
package lib
trait Trait {
type T
def foo(t: T): T
def bar(t: T): T = foo(t)
}
now, instead of defining bar in Trait,
I want to defined it somewhere else
(maybe for didactical reasons)
... anyway ...
so I write the following application code
package com.imaginej
package app
object Object1 {
import lib.Trait._
def bar(t: T): T = foo(t)
}
ok, this code does not compile
(and I can live with that)
so here is a next try
package com.imaginej
package app
object Object2 {
import lib.Trait
def bar(t: Trait#T): Trait#T = Trait#foo(t)
}
now the compiler does not complain about
Trait#T, but he complains about Trait#foo
Is there a way to keep the compiler happy?
Luc
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
suppose I have the following library code
package com.imaginej
package lib
trait Trait {
type T
def foo(t: T): T
def bar(t: T): T = foo(t)
}
now, instead of defining bar in Trait,
I want to defined it somewhere else
(maybe for didactical reasons)
... anyway ...
so I write the following application code
package com.imaginej
package app
object Object1 {
import lib.Trait._
def bar(t: T): T = foo(t)
}
ok, this code does not compile
(and I can live with that)
so here is a next try
package com.imaginej
package app
object Object2 {
import lib.Trait
def bar(t: Trait#T): Trait#T = Trait#foo(t)
}
now the compiler does not complain about
Trait#T, but he complains about Trait#foo
Is there a way to keep the compiler happy?
Luc
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
Thu, 2011-04-14, 14:07
#2
Re: simple language question
thanks for the reply
ps: yes importing the trait compiled (?)
$ cat src/Trait.scala
package com.imaginej
package lib
trait Trait {
type T
def foo: T => T
def bar: T => T = foo
}
$ cat src/Object2.scala
package com.imaginej
package app
object Object2 {
import lib.Trait
type TT = Trait#T
}
$ fsc -d bin src/Trait.scala src/Object2.scala
$
On Thu, Apr 14, 2011 at 2:29 PM, Sciss <contact@sciss.de> wrote:
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
ps: yes importing the trait compiled (?)
$ cat src/Trait.scala
package com.imaginej
package lib
trait Trait {
type T
def foo: T => T
def bar: T => T = foo
}
$ cat src/Object2.scala
package com.imaginej
package app
object Object2 {
import lib.Trait
type TT = Trait#T
}
$ fsc -d bin src/Trait.scala src/Object2.scala
$
On Thu, Apr 14, 2011 at 2:29 PM, Sciss <contact@sciss.de> wrote:
i don't think you can import a trait -- does the import line compile with you??
you can only import values. for instance this works for me:
trait Trait {
type T
def foo(t: T): T
// def bar(t: T): T = foo(t)
}
val x = new Trait { type T = Int; def foo(t: Int ) : Int = -t }
object Object1 {
import x._
def bar(t: T): T = foo(t)
}
?
best, -sciss-
On 14 Apr 2011, at 13:21, Luc Duponcheel wrote:
> Hello,
>
> suppose I have the following library code
>
> package com.imaginej
>
> package lib
>
> trait Trait {
> type T
> def foo(t: T): T
> def bar(t: T): T = foo(t)
> }
>
>
> now, instead of defining bar in Trait,
> I want to defined it somewhere else
> (maybe for didactical reasons)
>
> ... anyway ...
>
> so I write the following application code
>
> package com.imaginej
>
> package app
>
> object Object1 {
> import lib.Trait._
> def bar(t: T): T = foo(t)
> }
>
>
> ok, this code does not compile
> (and I can live with that)
>
> so here is a next try
>
> package com.imaginej
>
> package app
>
> object Object2 {
> import lib.Trait
> def bar(t: Trait#T): Trait#T = Trait#foo(t)
> }
>
> now the compiler does not complain about
> Trait#T, but he complains about Trait#foo
>
>
> Is there a way to keep the compiler happy?
>
>
> Luc
>
> --
> __~O
> -\ <,
> (*)/ (*)
>
> reality goes far beyond imagination
>
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
Thu, 2011-04-14, 14:17
#3
Re: simple language question
by import lib.Trait you are not importing anything but the type Trait into your namespace. You will thus only be able to refer to its type members, such as Trait#T but not to any methods because you can only invoke methods on a value of that type. how do you imagine calling Trait#foo without having an instance of Trait? that clearly doesn't make sense. The hash operator # is just for referring to types not field or method members AFAIK.
best, -sciss-
On 14 Apr 2011, at 14:06, Luc Duponcheel wrote:
> thanks for the reply
>
> ps: yes importing the trait compiled (?)
>
> $ cat src/Trait.scala
> package com.imaginej
>
> package lib
>
> trait Trait {
> type T
> def foo: T => T
> def bar: T => T = foo
> }
> $ cat src/Object2.scala
> package com.imaginej
>
> package app
> object Object2 {
> import lib.Trait
> type TT = Trait#T
> }
>
> $ fsc -d bin src/Trait.scala src/Object2.scala
> $
>
>
>
> On Thu, Apr 14, 2011 at 2:29 PM, Sciss wrote:
> i don't think you can import a trait -- does the import line compile with you??
>
> you can only import values. for instance this works for me:
>
> trait Trait {
> type T
> def foo(t: T): T
> // def bar(t: T): T = foo(t)
> }
>
> val x = new Trait { type T = Int; def foo(t: Int ) : Int = -t }
>
> object Object1 {
> import x._
> def bar(t: T): T = foo(t)
> }
>
> ?
>
> best, -sciss-
>
>
> On 14 Apr 2011, at 13:21, Luc Duponcheel wrote:
>
> > Hello,
> >
> > suppose I have the following library code
> >
> > package com.imaginej
> >
> > package lib
> >
> > trait Trait {
> > type T
> > def foo(t: T): T
> > def bar(t: T): T = foo(t)
> > }
> >
> >
> > now, instead of defining bar in Trait,
> > I want to defined it somewhere else
> > (maybe for didactical reasons)
> >
> > ... anyway ...
> >
> > so I write the following application code
> >
> > package com.imaginej
> >
> > package app
> >
> > object Object1 {
> > import lib.Trait._
> > def bar(t: T): T = foo(t)
> > }
> >
> >
> > ok, this code does not compile
> > (and I can live with that)
> >
> > so here is a next try
> >
> > package com.imaginej
> >
> > package app
> >
> > object Object2 {
> > import lib.Trait
> > def bar(t: Trait#T): Trait#T = Trait#foo(t)
> > }
> >
> > now the compiler does not complain about
> > Trait#T, but he complains about Trait#foo
> >
> >
> > Is there a way to keep the compiler happy?
> >
> >
> > Luc
> >
> > --
> > __~O
> > -\ <,
> > (*)/ (*)
> >
> > reality goes far beyond imagination
> >
>
>
>
>
Thu, 2011-04-14, 14:27
#4
Re: simple language question
... okay, so now I'm confused. What does this code *do*? What do you expect it to do? I mean, T is essentially a parameter for the trait, and in this situation I have no idea how one would ever resolve that parameter. In the usual mix-in situation for traits it's clear how you resolve that, but this sort of behind-the-back importing seems to short-circuit that.
I'm genuinely surprised that this code compiles, and really curious whether any of the Scala experts can provide any insight into what's going on here. Is Trait#T actually meaningful here?
On Thu, Apr 14, 2011 at 9:06 AM, Luc Duponcheel <luc.duponcheel@gmail.com> wrote:
I'm genuinely surprised that this code compiles, and really curious whether any of the Scala experts can provide any insight into what's going on here. Is Trait#T actually meaningful here?
On Thu, Apr 14, 2011 at 9:06 AM, Luc Duponcheel <luc.duponcheel@gmail.com> wrote:
thanks for the reply
ps: yes importing the trait compiled (?)
$ cat src/Trait.scala
package com.imaginej
package lib
trait Trait {
type T
def foo: T => T
def bar: T => T = foo
}
$ cat src/Object2.scala
package com.imaginej
package app
object Object2 {
import lib.Trait
type TT = Trait#T
}
$ fsc -d bin src/Trait.scala src/Object2.scala
$
On Thu, Apr 14, 2011 at 2:29 PM, Sciss <contact@sciss.de> wrote:
i don't think you can import a trait -- does the import line compile with you??
you can only import values. for instance this works for me:
trait Trait {
type T
def foo(t: T): T
// def bar(t: T): T = foo(t)
}
val x = new Trait { type T = Int; def foo(t: Int ) : Int = -t }
object Object1 {
import x._
def bar(t: T): T = foo(t)
}
?
best, -sciss-
On 14 Apr 2011, at 13:21, Luc Duponcheel wrote:
> Hello,
>
> suppose I have the following library code
>
> package com.imaginej
>
> package lib
>
> trait Trait {
> type T
> def foo(t: T): T
> def bar(t: T): T = foo(t)
> }
>
>
> now, instead of defining bar in Trait,
> I want to defined it somewhere else
> (maybe for didactical reasons)
>
> ... anyway ...
>
> so I write the following application code
>
> package com.imaginej
>
> package app
>
> object Object1 {
> import lib.Trait._
> def bar(t: T): T = foo(t)
> }
>
>
> ok, this code does not compile
> (and I can live with that)
>
> so here is a next try
>
> package com.imaginej
>
> package app
>
> object Object2 {
> import lib.Trait
> def bar(t: Trait#T): Trait#T = Trait#foo(t)
> }
>
> now the compiler does not complain about
> Trait#T, but he complains about Trait#foo
>
>
> Is there a way to keep the compiler happy?
>
>
> Luc
>
> --
> __~O
> -\ <,
> (*)/ (*)
>
> reality goes far beyond imagination
>
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
Thu, 2011-04-14, 15:07
#5
Re: simple language question
On Thu, Apr 14, 2011 at 7:21 AM, Luc Duponcheel <luc.duponcheel@gmail.com> wrote:
foo is a method on Trait, of which you have no instance.
foo is still an instance method on Trait, of which you have no instance.
Give it an instance of Trait?
object Object1 {
import lib.Trait._
def bar(t: T): T = foo(t)
}
foo is a method on Trait, of which you have no instance.
object Object2 {
import lib.Trait
def bar(t: Trait#T): Trait#T = Trait#foo(t)
}
foo is still an instance method on Trait, of which you have no instance.
Is there a way to keep the compiler happy?
Give it an instance of Trait?
Thu, 2011-04-14, 15:37
#6
Re: simple language question
thx,
you're absolutely right,
and now I also understand why Trait#T was not an issue
(no instance needed for that)
for Justin:
indeed eventually I need to define the type T (and foo) somewhere,
but I wanted to wait as long as possible
bottom line:
if the stuff generic it belongs to the trait anyway
if it is specific then it should be defines using an instance
thx all
Luc
On Thu, Apr 14, 2011 at 3:46 PM, Nils Kilden-Pedersen <nilskp@gmail.com> wrote:
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
you're absolutely right,
and now I also understand why Trait#T was not an issue
(no instance needed for that)
for Justin:
indeed eventually I need to define the type T (and foo) somewhere,
but I wanted to wait as long as possible
bottom line:
if the stuff generic it belongs to the trait anyway
if it is specific then it should be defines using an instance
thx all
Luc
On Thu, Apr 14, 2011 at 3:46 PM, Nils Kilden-Pedersen <nilskp@gmail.com> wrote:
On Thu, Apr 14, 2011 at 7:21 AM, Luc Duponcheel <luc.duponcheel@gmail.com> wrote:object Object1 {
import lib.Trait._
def bar(t: T): T = foo(t)
}
foo is a method on Trait, of which you have no instance.
object Object2 {
import lib.Trait
def bar(t: Trait#T): Trait#T = Trait#foo(t)
}
foo is still an instance method on Trait, of which you have no instance.
Is there a way to keep the compiler happy?
Give it an instance of Trait?
--
__~O
-\ <,
(*)/ (*)
reality goes far beyond imagination
i don't think you can import a trait -- does the import line compile with you??
you can only import values. for instance this works for me:
trait Trait {
type T
def foo(t: T): T
// def bar(t: T): T = foo(t)
}
val x = new Trait { type T = Int; def foo(t: Int ) : Int = -t }
object Object1 {
import x._
def bar(t: T): T = foo(t)
}
?
best, -sciss-
On 14 Apr 2011, at 13:21, Luc Duponcheel wrote:
> Hello,
>
> suppose I have the following library code
>
> package com.imaginej
>
> package lib
>
> trait Trait {
> type T
> def foo(t: T): T
> def bar(t: T): T = foo(t)
> }
>
>
> now, instead of defining bar in Trait,
> I want to defined it somewhere else
> (maybe for didactical reasons)
>
> ... anyway ...
>
> so I write the following application code
>
> package com.imaginej
>
> package app
>
> object Object1 {
> import lib.Trait._
> def bar(t: T): T = foo(t)
> }
>
>
> ok, this code does not compile
> (and I can live with that)
>
> so here is a next try
>
> package com.imaginej
>
> package app
>
> object Object2 {
> import lib.Trait
> def bar(t: Trait#T): Trait#T = Trait#foo(t)
> }
>
> now the compiler does not complain about
> Trait#T, but he complains about Trait#foo
>
>
> Is there a way to keep the compiler happy?
>
>
> Luc
>