const functor (or parameterized type)

26 views
Skip to first unread message

Vlad Patryshev

unread,
Jun 3, 2012, 5:27:00 PM6/3/12
to scala-user
So, there seems to be no way to define a constant functor (or a constant parameterized type), or am I wrong?

class ConstantFunctor[T[_](value: T[???]) extends Functor[T] {
  def map[A, B](f: (A) => B) = value
}

Scalaz has a special class called Const, but I was kind of lost trying to figure out how it could be useful.

Or is it all thanks to Free Theorems? How about phantom types then...

Thanks,
-Vlad

Vlad Patryshev

unread,
Jun 3, 2012, 6:38:39 PM6/3/12
to scala-user
Sorry, I meant something like this:

abstract class ConstantFunctor[T[_], T0 <: T[Any]] extends Functor[T] {
  def f1[A, B](f: (A) => B) = (identity[T0]_).asInstanceOf[T[A] => T[B]]
}


Thanks,
-Vlad

Tony Morris

unread,
Jun 3, 2012, 7:03:33 PM6/3/12
to scala...@googlegroups.com
apocalisp.wordpress.com/2010/04/21/a-proper-constant-function-in-scala/

On 04/06/12 08:38, Vlad Patryshev wrote:
> Sorry, I meant something like this:
>
> *abstract class ConstantFunctor[T[_], T0 <: T[Any]] extends Functor[T] {*
> * def f1[A, B](f: (A) => B) = (identity[T0]_).asInstanceOf[T[A] => T[B]]*
> *}*
>
>
> Thanks,
> -Vlad
>
>
> On Sun, Jun 3, 2012 at 2:27 PM, Vlad Patryshev <vpatr...@gmail.com> wrote:
>
>> So, there seems to be no way to define a constant functor (or a constant
>> parameterized type), or am I wrong?
>>
>> class ConstantFunctor[T[_](value: T[???]) extends Functor[T] {
>> def map[A, B](f: (A) => B) = value
>> }
>>
>> Scalaz has a special class called Const, but I was kind of lost trying to
>> figure out how it could be useful.
>>
>> Or is it all thanks to Free Theorems? How about phantom types then...
>>
>> Thanks,
>> -Vlad
>>


--
Tony Morris
http://tmorris.net/


Reply all
Reply to author
Forward
0 new messages