- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Type alias over type constructors
Mon, 2011-08-08, 10:05
Hi,
The following doesn't seem to be possible:
trait OddFunctor[A] {
type T
def unit(v: A) : T[A]
def fmap[B](f: A => B): T[B]
}
class SomeClass[T]( val v : T) extends OddFunctor[SomeClass] {
type T = SomeClass
def unit[B](v: B): T[B] = ...
def fmap[B](f: A => B): T[B] = ...
}
Compiler complains that SomeClass takes type parameters.
Is there away to workaround this?
P.S. This isn't really the way I'd write Functors but rather using higher kinded types. This is just an exercise.
Thanks,
Marius
The following doesn't seem to be possible:
trait OddFunctor[A] {
type T
def unit(v: A) : T[A]
def fmap[B](f: A => B): T[B]
}
class SomeClass[T]( val v : T) extends OddFunctor[SomeClass] {
type T = SomeClass
def unit[B](v: B): T[B] = ...
def fmap[B](f: A => B): T[B] = ...
}
Compiler complains that SomeClass takes type parameters.
Is there away to workaround this?
P.S. This isn't really the way I'd write Functors but rather using higher kinded types. This is just an exercise.
Thanks,
Marius
Mon, 2011-08-08, 11:07
#2
Re: Type alias over type constructors
I see, although having type T[x] = SomeClass[x] would cause another compile time error for
def unit(v: A) : T[A]
saying that T does not take type parameters. Which is normal I guess since T[x] is a proper type.
Thanks,
Marius
On Mon, Aug 8, 2011 at 12:53 PM, Adriaan Moors <adriaan.moors@epfl.ch> wrote:
def unit(v: A) : T[A]
saying that T does not take type parameters. Which is normal I guess since T[x] is a proper type.
Thanks,
Marius
On Mon, Aug 8, 2011 at 12:53 PM, Adriaan Moors <adriaan.moors@epfl.ch> wrote:
On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius.danciu@gmail.com> wrote:type T = SomeClasstype T[x] = SomeClass[x]
this defines T as a type constructor of kind * -> *since we don't have kind annotations, when you simply write `type T = ...`, the kind * is inferred for this definition
our approach to defining types and type constructors avoids talking about kinds, like method definitions avoid talking about function types: think of the analogy between how you define methods by "prototypically" specifying the arguments they take, rather than giving the equivalent function type for the value:
def foo(x: Any): Any = ... // a prototype for valid calls of foo: must look like foo(x) where x is an Anytype Foo[x] = SomeClass[x] // a prototype for creating a proper type (i.e., of kind *): it must look like Foo[x], where x is some proper type
val foo: Any => Any = ... // Function-type corresponding to the method// type Foo : * -> * = SomeClass // not valid Scala (we don't like to talk about kinds)
Mon, 2011-08-08, 11:57
#3
Re: Type alias over type constructors
that's probably because you also have a type parameter T in scope (which doesn't take type parameters), a type parameter takes precedence over a type member (since the latter can be accessed by selecting it on `this` explicitly, whereas the former cannot)
On Mon, Aug 8, 2011 at 11:58 AM, Marius Danciu <marius.danciu@gmail.com> wrote:
On Mon, Aug 8, 2011 at 11:58 AM, Marius Danciu <marius.danciu@gmail.com> wrote:
I see, although having type T[x] = SomeClass[x] would cause another compile time error for
def unit(v: A) : T[A]
saying that T does not take type parameters. Which is normal I guess since T[x] is a proper type.
Thanks,
Marius
On Mon, Aug 8, 2011 at 12:53 PM, Adriaan Moors <adriaan.moors@epfl.ch> wrote:
On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius.danciu@gmail.com> wrote:type T = SomeClasstype T[x] = SomeClass[x]
this defines T as a type constructor of kind * -> *since we don't have kind annotations, when you simply write `type T = ...`, the kind * is inferred for this definition
our approach to defining types and type constructors avoids talking about kinds, like method definitions avoid talking about function types: think of the analogy between how you define methods by "prototypically" specifying the arguments they take, rather than giving the equivalent function type for the value:
def foo(x: Any): Any = ... // a prototype for valid calls of foo: must look like foo(x) where x is an Anytype Foo[x] = SomeClass[x] // a prototype for creating a proper type (i.e., of kind *): it must look like Foo[x], where x is some proper type
val foo: Any => Any = ... // Function-type corresponding to the method// type Foo : * -> * = SomeClass // not valid Scala (we don't like to talk about kinds)
Mon, 2011-08-08, 12:07
#4
Re: Type alias over type constructors
Indeed this works:
trait OddFunctor[A] {
type T[X]
def unit[B](v: B) : T[B]
def fmap[B](f: A => B): T[B]
}
class SomeClass[A]( val v : A) extends OddFunctor[A] {
type T[X] = SomeClass[X]
def unit[B](v: B): T[B] = new SomeClass(v)
def fmap[B](f: A => B): T[B] = unit(f(v))
}
Thank you very much Adrian.
Marius
On Mon, Aug 8, 2011 at 1:51 PM, Adriaan Moors <adriaan.moors@epfl.ch> wrote:
trait OddFunctor[A] {
type T[X]
def unit[B](v: B) : T[B]
def fmap[B](f: A => B): T[B]
}
class SomeClass[A]( val v : A) extends OddFunctor[A] {
type T[X] = SomeClass[X]
def unit[B](v: B): T[B] = new SomeClass(v)
def fmap[B](f: A => B): T[B] = unit(f(v))
}
Thank you very much Adrian.
Marius
On Mon, Aug 8, 2011 at 1:51 PM, Adriaan Moors <adriaan.moors@epfl.ch> wrote:
that's probably because you also have a type parameter T in scope (which doesn't take type parameters), a type parameter takes precedence over a type member (since the latter can be accessed by selecting it on `this` explicitly, whereas the former cannot)
On Mon, Aug 8, 2011 at 11:58 AM, Marius Danciu <marius.danciu@gmail.com> wrote:I see, although having type T[x] = SomeClass[x] would cause another compile time error for
def unit(v: A) : T[A]
saying that T does not take type parameters. Which is normal I guess since T[x] is a proper type.
Thanks,
Marius
On Mon, Aug 8, 2011 at 12:53 PM, Adriaan Moors <adriaan.moors@epfl.ch> wrote:
On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius.danciu@gmail.com> wrote:type T = SomeClasstype T[x] = SomeClass[x]
this defines T as a type constructor of kind * -> *since we don't have kind annotations, when you simply write `type T = ...`, the kind * is inferred for this definition
our approach to defining types and type constructors avoids talking about kinds, like method definitions avoid talking about function types: think of the analogy between how you define methods by "prototypically" specifying the arguments they take, rather than giving the equivalent function type for the value:
def foo(x: Any): Any = ... // a prototype for valid calls of foo: must look like foo(x) where x is an Anytype Foo[x] = SomeClass[x] // a prototype for creating a proper type (i.e., of kind *): it must look like Foo[x], where x is some proper type
val foo: Any => Any = ... // Function-type corresponding to the method// type Foo : * -> * = SomeClass // not valid Scala (we don't like to talk about kinds)
On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius.danciu@gmail.com> wrote:
type T[x] = SomeClass[x]
this defines T as a type constructor of kind * -> *since we don't have kind annotations, when you simply write `type T = ...`, the kind * is inferred for this definition
our approach to defining types and type constructors avoids talking about kinds, like method definitions avoid talking about function types: think of the analogy between how you define methods by "prototypically" specifying the arguments they take, rather than giving the equivalent function type for the value:
def foo(x: Any): Any = ... // a prototype for valid calls of foo: must look like foo(x) where x is an Anytype Foo[x] = SomeClass[x] // a prototype for creating a proper type (i.e., of kind *): it must look like Foo[x], where x is some proper type
val foo: Any => Any = ... // Function-type corresponding to the method// type Foo : * -> * = SomeClass // not valid Scala (we don't like to talk about kinds)