1

I want to use tuple returned from a method to make a new hashmap item but it gives me error when I write like this

var data= HashMap[String,String]()
data.update(choose("name"))

def choose(a:String):(String,String)= return (a, "Pete")

How do you use tuple to update the hashmap?

Eclipse IDE tells me "not enough arguments for method update: (key: String, value: String)Unit. Unspecified value parameter value." and won't let me compile the script.

1
  • Because data.update() does not accept a tuple. Commented Mar 7, 2012 at 1:25

2 Answers 2

6

Instead of

// update requires a separate parameters for key and value
data.update(choose("name")) // won't compile !

// the following will work
val (key,value) = choose("name")
data.update (key,value)

Use

data+=choose("name") // += takes (key,value) tuple as a parameter
Sign up to request clarification or add additional context in comments.

Comments

6

This works as well:

(data.update _).tupled(choose("name"))

tupled converts a function requiring multiple arguments into a function requiring a tuple of arguments.

In your case, it's not a clean as Vlad's approach, but, in general, tupled can be used when you have a tuple containing your arguments.

val f = (a: String, b: String, c: String) => a + b + c
val myargs = ("1", "2", "3")
println(f.tupled(myargs)) // produces "123"

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.