This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Type system hackery question (has this been fixed for 2.8?)

5 replies
Paul Chiusano
Joined: 2009-01-01,
User offline. Last seen 42 years 45 weeks ago.

Hello, I have a class which is basically an HList, but instead of each type being an A, it is a Foo[A] { def get: A; ... other methods }. I'll call this a FooList. I'd like to be able to convert between a FooList and an HList by calling .get on each element in the FooList and I want the result to be of the proper type. Here's the FooList defintion: 

 
sealed trait FooList {
case class FooNil extends FooList
case class FooCons[H,T<:FooList](head: Foo[H], tail: T) extends FooList
 
Ok, so far so good - now to allow conversion between this and an HList - I added an abstract type member to FooList, named HListForm, to store the corresponding HList type for each FooList. My evil plan was to simply convert the FooList to a List[Any] (simple), then call map(_.get) on this list, convert it to an HList, and then finally cast it to the  type HListForm:

 
sealed trait FooList {
   type HListForm <: HList
}
case class FooNil extends FooList {
type HListForm = HNil }
case class FooCons[H,T<:FooList](head: Foo[H], tail: T) { type HListForm = HCons[H,T#HListForm] }
 
... except I get an error "illegal cyclic reference involving type HListForm". I understand there's a ticket to "Allow recursive type projections" (http://lampsvn.epfl.ch/trac/scala/ticket/1291) but I'm not sure this is the same issue.

 
For reference, here's the HList impl - (somewhat impoverished)
 
sealed trait HList
case class HNil extends HList
case class HCons[H,T<:HList](head: H, tail: T) extends HList
 
Paul

Paul Chiusano
Joined: 2009-01-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Type system hackery question (has this been fixed for 2.8?)
Also, I am open to other suggestions for how to convert between the two types of lists. Perhaps my FooList could just be a regular HList, but with some sort of additional information that would allow me to call Foo.get on each member to return a new HList? I am kind of stumped.

On Wed, Apr 15, 2009 at 7:51 PM, Paul Chiusano <paul.chiusano@gmail.com> wrote:
Hello, I have a class which is basically an HList, but instead of each type being an A, it is a Foo[A] { def get: A; ... other methods }. I'll call this a FooList. I'd like to be able to convert between a FooList and an HList by calling .get on each element in the FooList and I want the result to be of the proper type. Here's the FooList defintion:    sealed trait FooList { case class FooNil extends FooList case class FooCons[H,T<:FooList](head: Foo[H], tail: T) extends FooList   Ok, so far so good - now to allow conversion between this and an HList - I added an abstract type member to FooList, named HListForm, to store the corresponding HList type for each FooList. My evil plan was to simply convert the FooList to a List[Any] (simple), then call map(_.get) on this list, convert it to an HList, and then finally cast it to the  type HListForm:   sealed trait FooList {    type HListForm <: HList } case class FooNil extends FooList { type HListForm = HNil } case class FooCons[H,T<:FooList](head: Foo[H], tail: T) { type HListForm = HCons[H,T#HListForm] }   ... except I get an error "illegal cyclic reference involving type HListForm". I understand there's a ticket to "Allow recursive type projections" (http://lampsvn.epfl.ch/trac/scala/ticket/1291) but I'm not sure this is the same issue.   For reference, here's the HList impl - (somewhat impoverished)   sealed trait HList case class HNil extends HList case class HCons[H,T<:HList](head: H, tail: T) extends HList   Paul

Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Type system hackery question (has this been fixed for 2.8?)

Paul Chiusano wrote:
> ... except I get an error "illegal cyclic reference involving type
> HListForm". I understand there's a ticket to "Allow recursive type
> projections" (http://lampsvn.epfl.ch/trac/scala/ticket/1291) but I'm not
> sure this is the same issue.

Have you tried the -Yrecursion switch?

There's an implementation of HList in Scala at:

http://trac.assembla.com/metascala/browser/src/metascala/HLists.scala

Maybe that can help you.

/Jesper Nordenberg

Szymon Jachim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Type system hackery question (has this been fixed for
Is there a chance that this type system feature will to be turned on by default in 2.8?

Szymon

On Thu, Apr 16, 2009 at 10:22 AM, Jesper Nordenberg <megagurka@yahoo.com> wrote:
Paul Chiusano wrote:
... except I get an error "illegal cyclic reference involving type HListForm". I understand there's a ticket to "Allow recursive type projections" (http://lampsvn.epfl.ch/trac/scala/ticket/1291) but I'm not sure this is the same issue.

Have you tried the -Yrecursion switch?

There's an implementation of HList in Scala at:

http://trac.assembla.com/metascala/browser/src/metascala/HLists.scala

Maybe that can help you.

/Jesper Nordenberg




--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Re: Re: Type system hackery question (has this been fixed

On Thu, Apr 16, 2009 at 12:53 PM, Szymon Jachim wrote:
> Is there a chance that this type system feature will to be turned on by
> default in 2.8?
>
I doubt it. We still need to think things through thoroughly to be
clear of all the possible consequences before we fiddle with that in
the defaults. And there's lots of other things to do for 2.8. Maybe
sometimes after that...

Cheers

Paul Chiusano
Joined: 2009-01-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Type system hackery question (has this been fixed for
-Yrecursion worked for me with 2.7.4 RC. I can't figure out how to enable that flag in the NetBeans plugin though.   On Thu, Apr 16, 2009 at 4:22 AM, Jesper Nordenberg <megagurka@yahoo.com> wrote:
Paul Chiusano wrote:
... except I get an error "illegal cyclic reference involving type HListForm". I understand there's a ticket to "Allow recursive type projections" (http://lampsvn.epfl.ch/trac/scala/ticket/1291) but I'm not sure this is the same issue.

Have you tried the -Yrecursion switch?

There's an implementation of HList in Scala at:

http://trac.assembla.com/metascala/browser/src/metascala/HLists.scala

Maybe that can help you.

/Jesper Nordenberg


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland