Ignatius Monthy
Ignatius Monthy

Reputation: 117

Scala Type inference for anonymous function declaration

I'm a beginner in Scala and I'm just curious about how Scala handles the type inference for this code snippet

trait Expression { .... }

def eval (binding : String => Boolean) : Expression => Boolean

I understand that binding is a function that converts String to Boolean, but why binding at the same time could be declared as a member of Expression? is it implicitly converted? How does it work?

Sorry if my question is a bit confusing

Thanks so much :D

Upvotes: 1

Views: 436

Answers (2)

Hongfei
Hongfei

Reputation: 143

I think the key point is that, function eval returns a function, whose type is Function2[Expression, Boolean].

It's more clear to say:

def eval (binding : String => Boolean) : (Expression => Boolean)

There is no direct relationship between binding and Expression.

Upvotes: 4

Alexey Romanov
Alexey Romanov

Reputation: 170899

There is absolutely no type inference going on here, as Jörg W Mittag says.

def eval (binding : String => Boolean) : Expression => Boolean

is simply an abstract method declaration (abstract because it doesn't have a body). It can be implemented in different ways, depending on the definition of Expression.

why binding at the same time could be declared as a member of Expression

It can't, given just what you posted.

Upvotes: 5

Related Questions