# Type inference question

5 replies
Georgios Gousios
Joined: 2011-11-21,
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?
paulbutcher
Joined: 2010-03-08,
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++

Who says I have a one track mind?

http://www.paulbutcher.com/
MSN: paul [at] paulbutcher [dot] com
AIM: paulrabutcher
Skype: paulrabutcher
Georgios Gousios
Joined: 2011-11-21,
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,
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

extempore
Joined: 2008-12-17,
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,
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. :)