The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Scala Tuples and Map+=

2 replies on 1 page. Most recent reply: May 18, 2014 10:44 AM by Michael Hanafey

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Michael Dai

Posts: 2
Nickname: maketa521
Registered: May, 2014

Scala Tuples and Map+= Posted: May 4, 2014 4:08 AM
Reply to this message Reply
Advertisement
—————————————————————————
Code:
—————————————————————————
val pair = (99, "Luftballons")
// pair is a tuple, can be created like this.

import scala.collection.mutable.Map
val treasureMap = Map[Int, String]()
treasureMap += (1 -> "Go to island.")
treasureMap += (2 -> "Find big X on ground.")
treasureMap += (3 -> "Dig.")
// -> is a method to return a two-element tuple
// += needs a param of two-element tuple

treasureMap += (99 , "Luftballons")
// (99, "Luftballons") is a tuple, am I wrong?

————————————————————————
Compile out:
————————————————————————
error: type mismatch;
found : Int(99)
required: (Int, String)
treasureMap += (99 , "Luftballons")
^
one error found

————————————————————————
Statement
————————————————————————
(99 , "Luftballons") is not a tuple, right ?
but pair is a tuple.
So pair can be add to the map by using "+=" method.
So (99 , "Luftballons") needs a "=" method to do type casting, right?
Why this conversion can not be automatic?

New to Scala, found this question, troubled me :( help me plz


Michael Dai

Posts: 2
Nickname: maketa521
Registered: May, 2014

Re: Scala Tuples and Map+= Posted: May 4, 2014 4:52 AM
Reply to this message Reply
What a surprise to me is that I add a pair of brackets and then it got through.
————————————————————————————
The code is here:
————————————————————————————

val pair = (99, "Luftballons")
// pair is a tuple, can be created like this.

import scala.collection.mutable.Map
val treasureMap = Map[Int, String]()
treasureMap += (1 -> "Go to island.")
treasureMap += (2 -> "Find big X on ground.")
treasureMap += (3 -> "Dig.")
// -> is a method to return a two-element tuple
// += needs a param of two-element tuple

treasureMap += ((99 , "Luftballons"))
// Pay attention to the brackets outer (99,"Luftballons").
// Goes right. But how to explain?

————————————————————————————
Statement:
————————————————————————————
Well, I finally understand. When I tried to use println((99, "Luftballons")) for test. I realised the pair of outer brackets is the one of left method. As println(...) needs a (), += needs the same.
But why does this happen?
treasureMap += 3 -> "Dig." // with no brackets at all and it goes well
I think this is an syntactic sugar. The brackets are saved because of method +=.
But when it is added, first default recognised as the part of method +=.
So when we want to cast the 99,"..." to be a tuple, we must add another pair brackets.

Michael Hanafey

Posts: 1
Nickname: hanafey
Registered: May, 2014

Re: Scala Tuples and Map+= Posted: May 18, 2014 10:44 AM
Reply to this message Reply
Write it as an explicit method call and it becomes clear:

treasureMap.+=(99, "Second argument")
treasureMap.+=((99, "Value"))

In the first line the "+=" method is called with two arguments, and this is overloaded method does not exist.

However, you could also write:

treasureMap update (99, "Gold")

because this is equivalent to:

treasureMap.update(99, "Gold")

The syntax 99 -> "Value" works because this is a method call that returns a single value, a tuple.

Flat View: This topic has 2 replies on 1 page
Topic: How to get message from custom component? Previous Topic   Next Topic Topic: In search of deeper understanding of higher-function


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us