0

Is it possible to implement in Scala an implicit conversion for the group of parameters (without defining them as some class member) like

implicit def triple2One (x :Int, s :String, d :Double) =  x  // just as an example

So that I would be able to call it in the code like

val x :Int = (1, "test", 2.0)
3
  • 1
    From your example, I think that closest you can get to the desired result is by defining an implicit conversion for Tuple3[Int, String, Double] (can also be short-handed in the argument as arg : (Int, String, Double))... Commented Nov 29, 2013 at 17:16
  • Ok seems to be good. Tuples are limited to Tuple22 right? Commented Nov 29, 2013 at 17:19
  • 1
    Yup, same as FunctionN Commented Nov 29, 2013 at 17:19

1 Answer 1

3

It is possible:

scala> implicit def iFromISD(isd: (Int, String, Double)): Int = isd._1
iFromISD: (isd: (Int, String, Double))Int

scala> val x: Int = (1, "two", 3.0)
x: Int = 1

Naturally, there has to be a type annotation on the resulting val to drive the search for and application of the implicit conversion.

Addendum

It occurs to me there's another way that doesn't involve dubious implicit conversions:

scala> val (y, _, _) = (1, "two", 3.0)
y: Int = 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.