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

Trying to discover if a object is a tuple

9 replies
alberto_souza
Joined: 2010-02-19,
User offline. Last seen 32 weeks 5 days ago.
Hi, Is there a way to discover if a object is a tuple without use pattern matching with all possibilities?

Thanks,

Alberto
Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Trying to discover if a object is a tuple


.isInstanceOf[Product]

 will give you a hint, though not all produts are tuples....

 Cheers


On Mon, May 10, 2010 at 4:41 PM, Alberto SOUZA <alots.ssa@gmail.com> wrote:
Hi, Is there a way to discover if a object is a tuple without use pattern matching with all possibilities?

Thanks,

Alberto

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Trying to discover if a object is a tuple
x.getClass.getName.startsWith("scala.Tuple") ought to do the trick.
  --Rex

On Mon, May 10, 2010 at 4:41 PM, Alberto SOUZA <alots.ssa@gmail.com> wrote:
Hi, Is there a way to discover if a object is a tuple without use pattern matching with all possibilities?

Thanks,

Alberto

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Trying to discover if a object is a tuple

perhaps

scala> val Ptrn = """scala.Tuple(\d)+$?.*""".r
scala> def isTuple( x: AnyRef ) = if( x.isInstanceOf[ Product ]) x match {
| case Ptrn( i ) => Some( i.toInt )
| case _ => None
| } else None

isTuple( List( 3, 3 )) // --> None
isTuple( 3 -> 3 )) // --> Some(2)
isTuple( ('a, "b", 33) ) // --> Some(3)

of course, this is a kind of pattern match ;-)

best, -sciss-

Am 10.05.2010 um 22:00 schrieb Maxime Lévesque:

>
>
> .isInstanceOf[Product]
>
> will give you a hint, though not all produts are tuples....
>
> Cheers
>
>
> On Mon, May 10, 2010 at 4:41 PM, Alberto SOUZA wrote:
> Hi, Is there a way to discover if a object is a tuple without use pattern matching with all possibilities?
>
> Thanks,
>
> Alberto
>

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: Trying to discover if a object is a tuple

On Monday May 10 2010, Maxime Lévesque wrote:
> .isInstanceOf[Product]
>
> will give you a hint, though not all produts are tuples....

All case classes, e.g., are Product.

Randall Schulz

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Trying to discover if a object is a tuple

If you really want to know:

scala> val t = (1, 2, 3)
t: (Int, Int, Int) = (1,2,3)

scala> (1 to 22).toStream.map(n => Class.forName("scala.Tuple" +
n)).exists(cls => t.getClass isAssignableFrom cls)
res5: Boolean = true

On Mon, May 10, 2010 at 11:18 PM, Randall R Schulz wrote:
> On Monday May 10 2010, Maxime Lévesque wrote:
>> .isInstanceOf[Product]
>>
>>  will give you a hint, though not all produts are tuples....
>
> All case classes, e.g., are Product.
>
>
> Randall Schulz
>

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Trying to discover if a object is a tuple


On Mon, May 10, 2010 at 2:06 PM, Rex Kerr <ichoran@gmail.com> wrote:
x.getClass.getName.startsWith("scala.Tuple") ought to do the trick.

Unless someone passes you a subclass of Tuple.  Lift contains a lot of places where we subclass Tuple (see net.liftweb.util.BindHelpers)  
  --Rex

On Mon, May 10, 2010 at 4:41 PM, Alberto SOUZA <alots.ssa@gmail.com> wrote:
Hi, Is there a way to discover if a object is a tuple without use pattern matching with all possibilities?

Thanks,

Alberto




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Trying to discover if a object is a tuple
Of course this kind of code would tempt me to write a scala.Tuple666OhYeahGotcha

2010/5/10 Sciss <contact@sciss.de>
perhaps

scala> val Ptrn = """scala.Tuple(\d)+$?.*""".r
scala> def isTuple( x: AnyRef ) = if( x.isInstanceOf[ Product ]) x match {
    | case Ptrn( i ) => Some( i.toInt )
    | case _ => None
    | } else None

isTuple( List( 3, 3 )) // --> None
isTuple( 3 -> 3 )) // --> Some(2)
isTuple( ('a, "b", 33) ) // --> Some(3)

of course, this is a kind of pattern match ;-)

best, -sciss-


Am 10.05.2010 um 22:00 schrieb Maxime Lévesque:

>
>
> .isInstanceOf[Product]
>
>  will give you a hint, though not all produts are tuples....
>
>  Cheers
>
>
> On Mon, May 10, 2010 at 4:41 PM, Alberto SOUZA <alots.ssa@gmail.com> wrote:
> Hi, Is there a way to discover if a object is a tuple without use pattern matching with all possibilities?
>
> Thanks,
>
> Alberto
>




--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Trying to discover if a object is a tuple
On Mon, May 10, 2010 at 5:18 PM, David Pollak <feeder.of.the.bears@gmail.com> wrote:


On Mon, May 10, 2010 at 2:06 PM, Rex Kerr <ichoran@gmail.com> wrote:
x.getClass.getName.startsWith("scala.Tuple") ought to do the trick.

Unless someone passes you a subclass of Tuple.  Lift contains a lot of places where we subclass Tuple (see net.liftweb.util.BindHelpers)

You put them in the scala package, call them Tuplewhatever, and they are not Tuples?  Isn't that kind of unwise?

Anyway, one can always add extra flourishes to avoid this (though aside from searching through scala-library.jar, I don't see a way to make it bulletproof).

  --Rex

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Trying to discover if a object is a tuple
Crikey, I completely misinterpreted what you were saying; Sciss's answer looks much more on-target.
  --Rex

On Mon, May 10, 2010 at 6:07 PM, Rex Kerr <ichoran@gmail.com> wrote:
On Mon, May 10, 2010 at 5:18 PM, David Pollak <feeder.of.the.bears@gmail.com> wrote:


On Mon, May 10, 2010 at 2:06 PM, Rex Kerr <ichoran@gmail.com> wrote:
x.getClass.getName.startsWith("scala.Tuple") ought to do the trick.

Unless someone passes you a subclass of Tuple.  Lift contains a lot of places where we subclass Tuple (see net.liftweb.util.BindHelpers)

You put them in the scala package, call them Tuplewhatever, and they are not Tuples?  Isn't that kind of unwise?

Anyway, one can always add extra flourishes to avoid this (though aside from searching through scala-library.jar, I don't see a way to make it bulletproof).

  --Rex


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