Scala: why does List [=> Int] not work?

I am working on learning scala inputs and outputs, and recently I came across something that interests me.

As I understand it, if I want to pass a block of code that is effectively lazily evaluated by a function (without evaluating it in place), I can enter:

def run(a: =>Int):Int = {...} 

In this sense, the run function receives a block of code that remains to be evaluated, which it evaluates and returns the calculated Int. Then I tried to extend this idea to the List data structure. Input:

 def run(a: List[=>Int]) = {...} 

This, however, returns an error. I was wondering why this is forbidden. How, apart from this syntax, can I pass a list of unvalued blocks of code?

+8
scala lazy-evaluation
source share
2 answers

=>Int is the syntax for the name parameters. =>Int not a type, so it cannot be used as a parameter for List . However ()=>Int is a type. This is a type of null functions returning Int . So this works:

 def run(a: List[()=>Int]) = {...} 
+8
source share

All Articles