- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Trying to discover if a object is a tuple
Mon, 2010-05-10, 21:41
Hi, Is there a way to discover if a object is a tuple without use pattern matching with all possibilities?
Thanks,
Alberto
Thanks,
Alberto
Mon, 2010-05-10, 22:17
#2
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:
--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
Mon, 2010-05-10, 22:27
#3
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
>
Mon, 2010-05-10, 22:27
#4
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
Mon, 2010-05-10, 22:27
#5
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
>
Mon, 2010-05-10, 22:37
#6
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
Mon, 2010-05-10, 22:47
#7
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>
--
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
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
Mon, 2010-05-10, 23:17
#8
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:
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
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
Mon, 2010-05-10, 23:37
#9
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:
--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
.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: