Simple extension for Scala Tuples.

I’ve found this trivial extension for Scala tuples to be often very handy:

implicit def toTuple2x[T1, T2](t: Tuple2[T1, T2]) = new {
    def isSome = t._1 != null || t._2 != null
    def isNone = !isSome
    def isAll = t._1 != null && t._2 != null
    def opt1 = Option(t._1)
    def opt2 = Option(t._2)
}

We have it for Pairs and Triples and it makes it for a bit cleaner and more expressive tuple handling.

Enjoy!

2 responses

  1. I could be wrong, but it seems you can do all that for Product (which is the super class for Tuple2 and Tuple1 and any other newly created tuples) and use productArity, productElement, and productIterator.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: