Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 180057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:31:05+00:00 2026-05-11T14:31:05+00:00

I want to be able to implicitly convert Tuples of numbers (Ints and double)

  • 0

I want to be able to implicitly convert Tuples of numbers (Ints and double) into a vector object.

Assuming a Vector class with a + method

case class Vector(x: Double, y:Double){   def + (v:Vector)= new Vector(x+v.x,y+v.y) }  

My goal is to have the following code work.

val vec = (1,2)+(.5,.3) // vec == Vector(1.5,2.3) 

I can get it to work for Int with the following

implicit def int2vec(t:Tuple2[Int,Int])=new Vector(t._1,t._2) val vec = (1,2)+(3,4) // vec == Vector(4.0,6.0) 

But it fails when I add the convertion for double

implicit def int2vec(t:Tuple2[Int,Int])=new Vector(t._1,t._2) implicit def double2vec(t:Tuple2[Double,Double])=new Vector(t._1,t._2) val a = (1,2) val b = (.5,.3) val c = (1,1)+b // vec = Vector(1.5,1.3) val d = (1,2)+(.3,.5) // compile error: wrong number of arguments val e = (1,2)+((.3,.5)) // compile error: type mismatch 

Trying just double per Andri’s sugestion

implicit def double2vec(t:Tuple2[Double,Double])=new Vector(t._1,t._2) val a = (.5,.3) val b = (1,1)+a // type mismatch found:(Double,Double) required:String  

What do I need to do to get this to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:31:06+00:00Added an answer on May 11, 2026 at 2:31 pm

    Scala’s syntax is flexible, but it’s not infinitely flexible. In particular, the confluence of tuples, parameters, and implicits makes this a really dangerous area in the library design space. As you’ve noticed, things are likely to break, not work well, and give cryptic error messages. I’d suggest you avoid it if you can.

    In particular, I suggest you make the following definition:

    val V = Vector 

    And then all your examples work as you’d expect, without any implicits, magic, or cryptic error messages, and at the cost of only one character per Vector.

    val a = V(1,2)+V(.5,.3) val b = V(1,2)+V(3,4) val c = V(1,2) val d = V(.5,.3) val e = V(1,1)+b val f = V(1,2)+V(.3,.5) val g = V(.5,.3) val h = V(1,1)+a 

    It’s not exactly the syntax you want, but trust me, it’ll save you pain and headaches in the long-run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want my object to be able to type in an double or a
I want to be able to bind a single and double click event to
I want to be able to initialize a class just like I initialize a
I want to be able to define template <class TX> void f(const TX &x){
I want to call a partial method from outside of the declaring class. This
I'd like to be able to implicitly convert between two classes which are otherwise
class Person { string Name; int Age; } I want to be able to
I'm using LaTeX and BibTeX for an article, and I want to able to
Want to be able to provide a search interface for a collection of objects
I want to be able to take an image that i have already captured

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.