- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Type alias over type constructors
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










Re: Type alias over type constructors
On Mon, Aug 8, 2011 at 11:04 AM, Marius Danciu <marius [dot] danciu [at] gmail [dot] 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)
Re: Type alias over type constructors
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 [dot] moors [at] epfl [dot] ch> wrote:
Re: Type alias over type constructors
On Mon, Aug 8, 2011 at 11:58 AM, Marius Danciu <marius [dot] danciu [at] gmail [dot] com> wrote:
Re: Type alias over type constructors
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 [dot] moors [at] epfl [dot] ch> wrote: