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

Preserving Seq types

8 replies
Marcus Downing
Joined: 2011-02-08,
User offline. Last seen 42 years 45 weeks ago.
I'd like to have a method that takes a sequence (or monad) of some sort, and returns a sequence of the same sort:
    def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
If you give it a list, you get a list back; if you give it an option, you get an option, and so on. Is there any type trickery that makes this possible?
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Preserving Seq types

i know, i know!

def foo[T, M[T]](somethingWithSomething: M[T]): M[T] = {
null.asInstanceOf[M[T]]
}

val bar: Some[String] = foo(Some("String"))

-------- Original-Nachricht --------
> Datum: Mon, 19 Dec 2011 02:20:09 -0800 (PST)
> Von: Marcus Downing
> An: scala-user@googlegroups.com
> Betreff: [scala-user] Preserving Seq types

> I'd like to have a method that takes a sequence (or monad) of some sort,
> and returns a sequence of the same sort:
>
> def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
>
> If you give it a list, you get a list back; if you give it an option, you
> get an option, and so on. Is there any type trickery that makes this
> possible?

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Preserving Seq types
Sure you can, use the same tricks as the collections framework.
But it can get a bit involved...
import collection.generic.CanBuildFrom
def foo[CC[x] <: Seq[x], T](xs: CC[T])(implicit cbf: CanBuildFrom[CC[T],T,CC[T]]): CC[T] =   xs.map(identity)(collection.breakOut)
scala> foo(List(1,2,3)) res1: List[Int] = List(1, 2, 3)

On 19 December 2011 10:20, Marcus Downing <marcus.downing@gmail.com> wrote:
I'd like to have a method that takes a sequence (or monad) of some sort, and returns a sequence of the same sort:
    def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
If you give it a list, you get a list back; if you give it an option, you get an option, and so on. Is there any type trickery that makes this possible?



--
Kevin Wright
mail: kevin.wright@scalatechnology.com
gtalk / msn : kev.lee.wright@gmail.com quora: http://www.quora.com/Kevin-Wrightgoogle+: http://gplus.to/thecoda
kev.lee.wright@gmail.com twitter: @thecoda
vibe / skype: kev.lee.wrightsteam: kev_lee_wright
"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Preserving Seq types

this does only solve the type problem though.. option does not extend traversable, so you'll also need an implicit conversion form option to traversable for this to work.

and M should be "M <: Traversable[T]", but you'll figure that out ;)

-------- Original-Nachricht --------
> Datum: Mon, 19 Dec 2011 11:29:24 +0100
> Von: "Dennis Haupt"
> An: scala-user@googlegroups.com, scala-user@googlegroups.com
> Betreff: Re: [scala-user] Preserving Seq types

> i know, i know!
>
> def foo[T, M[T]](somethingWithSomething: M[T]): M[T] = {
> null.asInstanceOf[M[T]]
> }
>
> val bar: Some[String] = foo(Some("String"))
>
>
> -------- Original-Nachricht --------
> > Datum: Mon, 19 Dec 2011 02:20:09 -0800 (PST)
> > Von: Marcus Downing
> > An: scala-user@googlegroups.com
> > Betreff: [scala-user] Preserving Seq types
>
> > I'd like to have a method that takes a sequence (or monad) of some sort,
> > and returns a sequence of the same sort:
> >
> > def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
> >
> > If you give it a list, you get a list back; if you give it an option,
> you
> > get an option, and so on. Is there any type trickery that makes this
> > possible?

Marcus Downing
Joined: 2011-02-08,
User offline. Last seen 42 years 45 weeks ago.
Re: Preserving Seq types

Isn't the cast dangerous? Can I be absolutely certain that the type will always be correct?

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Preserving Seq types

the cast is in my code because i return null and for some reason i need to cast it (in contrast to java where null can just be assigned to any reference). in a real world use case, you should not need one.
traversable.filter(...) or whatever you want to do inside foo already returns the correct type (or should)

-------- Original-Nachricht --------
> Datum: Mon, 19 Dec 2011 03:45:53 -0800 (PST)
> Von: Marcus Downing
> An: scala-user@googlegroups.com
> Betreff: Re: [scala-user] Preserving Seq types

> Isn't the cast dangerous? Can I be absolutely certain that the type will
> always be correct?

Alec Zorab
Joined: 2010-05-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Preserving Seq types

So were you suggesting this:

def foo[T, M[T]](somethingWithSomething: M[T]): M[T] = {
sys.error("Todo")
}

val bar: Some[String] = foo(Some("String"))

?

Because that makes much more sense to me than casting nulls :)

On 19 December 2011 11:59, Dennis Haupt wrote:
> the cast is in my code because i return null and for some reason i need to cast it (in contrast to java where null can just be assigned to any reference). in a real world use case, you should not need one.
> traversable.filter(...) or whatever you want to do inside foo already returns the correct type (or should)
>
> -------- Original-Nachricht --------
>> Datum: Mon, 19 Dec 2011 03:45:53 -0800 (PST)
>> Von: Marcus Downing
>> An: scala-user@googlegroups.com
>> Betreff: Re: [scala-user] Preserving Seq types
>
>> Isn't the cast dangerous? Can I be absolutely certain that the type will
>> always be correct?

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Preserving Seq types

yes

-------- Original-Nachricht --------
> Datum: Mon, 19 Dec 2011 12:02:28 +0000
> Von: Alec Zorab
> An: Dennis Haupt
> CC: scala-user@googlegroups.com
> Betreff: Re: [scala-user] Preserving Seq types

> So were you suggesting this:
>
> def foo[T, M[T]](somethingWithSomething: M[T]): M[T] = {
> sys.error("Todo")
> }
>
> val bar: Some[String] = foo(Some("String"))
>
> ?
>
> Because that makes much more sense to me than casting nulls :)
>
> On 19 December 2011 11:59, Dennis Haupt wrote:
> > the cast is in my code because i return null and for some reason i need
> to cast it (in contrast to java where null can just be assigned to any
> reference). in a real world use case, you should not need one.
> > traversable.filter(...) or whatever you want to do inside foo already
> returns the correct type (or should)
> >
> > -------- Original-Nachricht --------
> >> Datum: Mon, 19 Dec 2011 03:45:53 -0800 (PST)
> >> Von: Marcus Downing
> >> An: scala-user@googlegroups.com
> >> Betreff: Re: [scala-user] Preserving Seq types
> >
> >> Isn't the cast dangerous? Can I be absolutely certain that the type
> will
> >> always be correct?

richard emberson
Joined: 2010-03-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Preserving Seq types

In general, not possible. See:
https://issues.scala-lang.org/browse/SI-5001

On 12/19/2011 02:20 AM, Marcus Downing wrote:
> I'd like to have a method that takes a sequence (or monad) of some sort,
> and returns a sequence of the same sort:
>
> def foo(list: Seq[T]): Seq[T] = list.filter(stuff)
>
> If you give it a list, you get a list back; if you give it an option,
> you get an option, and so on. Is there any type trickery that makes this
> possible?

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