Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type inference from right to left

Tags:

scala

I created following code snippet:

class PimpToRight[A](f: A => Boolean) {
  def <| (a: A) = f(a)
}
implicit def f2pimp[A](f: A => Boolean) = new PimpToRight(f)

class PimpToLeft[A](a: A) {
  def <|: (f: A => Boolean) = f(a)
  def |> (f: A => Boolean) = f(a)
}
implicit def a2pimp[A](a: A) = new PimpToLeft(a)

There are right- and left-associative methods available.

Following code works:

((_: Int) > 3) <| 7
((_: Int) > 3) <|: 7
7 |> (_ > 3)

But this does not:

(_ > 3) <| 7
(_ > 3) <|: 7

Is it possible to infer type parameters from right to left?

like image 287
kiritsuku Avatar asked Sep 27 '11 23:09

kiritsuku


People also ask

What is meant by type inference?

Type inference is the ability to automatically deduce, either partially or fully, the type of an expression at compile time. The compiler is often able to infer the type of a variable or the type signature of a function, without explicit type annotations having been given.

What is contextual typing?

Contextual typing occurs when the type of an expression is implied by its location. For example: window . onmousedown = function ( mouseEvent ) { console .

What is type inference in Swift?

Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide. Because of type inference, Swift requires far fewer type declarations than languages such as C or Objective-C.

What is type inference in Java?

Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable.


1 Answers

These two issues are related: SI-4773 and SI-1980. Based on these issues, the answer to your question seems to be no. While not directly related to your question, there is a very nice post by Paul Chiusano on making the most of type inference in Scala, which addresses the current state of type inference in Scala and provides some useful advice.

like image 129
Arjan Blokzijl Avatar answered Oct 06 '22 01:10

Arjan Blokzijl