Why do F# functions evaluate before they are called?

前端 未结 2 954
一个人的身影
一个人的身影 2020-12-20 00:25

If I define a module as such:

module Module1
open System

let foo =
    Console.WriteLine(\"bar\")

Then, in interactive do

         


        
2条回答
  •  抹茶落季
    2020-12-20 01:01

    I believe let foo = x is essentially a static value that gets evaluated immediately, once. If you want a parameterless function, you would need let foo () = Console.WriteLine("bar"), using it as foo ().

    If you don't want to have to use the parentheses when calling, something like type Test () = static member foo with get () = System.Console.WriteLine("bar"), using it as Test.foo should work.

提交回复
热议问题