This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Type inference question

5 replies
Georgios Gousios
Joined: 2011-11-21,
User offline. Last seen 42 years 45 weeks ago.
scala -versionScala code runner version 2.9.1.final -- Copyright 2002-2011, LAMP/EPFL
1. scala> val a = List(1,2,3)

a: List[Int] = List(1, 2, 3)

2. scala> val b = List(4,5,6)

b: List[Int] = List(4, 5, 6)

3. scala> a zip b

res18: List[(Int, Int)] = List((1,4), (2,5), (3,6))

4. scala> a.zip(b)

res19: List[(Int, Int)] = List((1,4), (2,5), (3,6))

5. scala> a.zip(b) ++ List((9, 10))

res17: List[(Int, Int)] = List((1,4), (2,5), (3,6), (9,10))

6. scala> a zip b ++ List((9, 10))

res15: List[(Int, Any)] = List((1,4), (2,5), (3,6))
Given the above REPL sequence, why do steps 5 and 6 produce different results (and in fact step 6 produces a wrong result)? Are there any differences in the way (or order?) types are inferenced in both cases?
Thanks in advance,Georgios
paulbutcher
Joined: 2010-03-08,
User offline. Last seen 10 weeks 5 days ago.
Re: Type inference question
On 21 Nov 2011, at 17:13, Georgios Gousios wrote:
6. scala> a zip b ++ List((9, 10))

res15: List[(Int, Any)] = List((1,4), (2,5), (3,6))
Given the above REPL sequence, why do steps 5 and 6 produce different results (and in fact step 6 produces a wrong result)? Are there any differences in the way (or order?) types are inferenced in both cases?

This is a simple precedence issue, isn't it? 
a zip b ++ List((9, 10))

is actually:
a zip (b ++ List((9, 10)))

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: paul [at] paulbutcher [dot] com
AIM: paulrabutcher
Skype: paulrabutcher
Georgios Gousios
Joined: 2011-11-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Type inference question

++ is a method in immutable.List, not an operator. Why would a chain of methods be evaluated from right to left?

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Type inference question


On Mon, Nov 21, 2011 at 6:24 PM, Georgios Gousios <gousiosg [at] gmail [dot] com> wrote:
++ is a method in immutable.List, not an operator. Why would a chain of methods be evaluated from right to left?

http://stackoverflow.com/questions/2922347/operator-precedence-in-scala

--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Type inference question

On Mon, Nov 21, 2011 at 9:13 AM, Georgios Gousios wrote:
> Given the above REPL sequence, why do steps 5 and 6 produce different
> results (and in fact step 6 produces a wrong result)?

A wrong result? Perish the thought. Looks right to me. Perhaps it's
clearer this way.

scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6)

scala> b ++ List((9, 10))
res1: List[Any] = List(4, 5, 6, (9,10))

scala> List(1, 2, 3) zip res1
res2: List[(Int, Any)] = List((1,4), (2,5), (3,6))

Todd Vierling
Joined: 2011-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Type inference question
On Monday, November 21, 2011 12:28:55 PM UTC-5, √iktor Klang wrote:
On Mon, Nov 21, 2011 at 6:24 PM, Georgios Gousios <gous [dot] [dot] [dot] [at] gmail [dot] com> wrote:
++ is a method in immutable.List, not an operator. Why would a chain of methods be evaluated from right to left?

http://stackoverflow.com/questions/2922347/operator-precedence-in-scala

More to the point, when opting for infix notation, methods become operators and are subject to operator precedence.
This is why I try _not_ to use too much infix notation: besides something like "a zip b ++ List(...)" being hard to read, it's not visually obvious which methods are being invoked on what objects, and in what order. Traditional use of dots and parens makes it crystal-clear.
Infix notation is a nice shortcut, but over-abusing it will cause you major pain, eventually. :)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland