Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Scala function types conform to the FunctionN traits?

Tags:

scala

In other words, what purpose do the FunctionN traits serve? Do they exist to enable the compiler to know how to treat functions as first class values? I.e. when a function accepts another function as an argument, FunctionN is needed by the compiler to do type-checking?

I want to make sure I understand the answer here correctly and unfortunately don't have enough reputation points to comment on the existing answer.

like image 884
Emily Chen Avatar asked Jun 03 '26 21:06

Emily Chen


1 Answers

what purpose do the FunctionN traits serve?

They serve the same purpose most traits do, to create structure and provide a common abstraction over concrete implementation. FunctionN is used by the underlying compiler when you use anonymous function syntax, and provides the implementation via it's apply method.

Do they exist to enable the compiler to know how to treat functions as first class values

Again, they exist to create a calling convention for functions in Scala, since methods don't have a value as far as the type system goes. They are the underlying implementation for the type system to express anonymous functions, which are first class values in the type system.

when a function accepts another function as an argument, FunctionN is needed by the compiler to do type-checking?

FunctionN is a type like any other, where type checking is being done like any other type. One thing to note us that the compiler is actually aware of the trait and underlying implementations since it has to utilize it for function values.

like image 111
Yuval Itzchakov Avatar answered Jun 07 '26 02:06

Yuval Itzchakov