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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With