I want to write a function that takes no arguments and returns no value.

May be this will be usefull

1
2
3
4
5
6
7
8
9
10
11
type t = static member hello_world_func = printfn "Hello, world!"

printfn "1"

t.hello_world_func

printfn "2"

t.hello_world_func

printfn "3"
By on 1/28/2009 2:43 AM ()

Hi,
if you write 'function' without arguments (as in your original example) then it becomes just a value declaration. This is executed only once (when initializing the value):

1
2
3
 
let foo = printfn "foo" // printed first 
printfn "next..." // printed after foo

The type of "foo" is "unit" which is an F# representation of "no-value". If you want to write a function that can be executed, but doesn't take any values as arguments, you can write a function that takes a unit (no-value) as an argument using the "()":

1
2
3
4
 
let foo() = printfn "foo" // doesn't print anything during declaration
printfn "now..." // printed before foo
foo()

The only value of type unit is "()". This means that when you call the "foo" you give it a unit as an argument (the type of "foo" is unit -> unit, which means that it expects one unit argument). The "()" in the declaration is a pattern that is matched with the unit value when the function is called (just like a parameter name, with the difference that you can't refer to the parameter - since unit doesn't carry any value, you don't need this anyway).

Note that unit is an ordinary value, so you can use it in many ways. The following isn't very useful, but it could help to clarify the idea:

1
2
3
4
5
let foo a =   // 'a' is parameter of type 'unit', but here we give it a name that can be used later
  if (a = ()) then printfn "foo"  // '()' is the only value, so this will be always true

let unitVal = () // create value 'unitVal' that contains the only unit value..
foo unitVal // call foo with unit as an argument

The syntax with "member" is slightly different thing. Member with no arguments is compiled as a .NET property and .NET properties are evaluated every time you access them (under the hood, there is a method "get" with type unit -> 'a where 'a is the type returned by the property). It is a good practice to keep properties very simple - they should just return a value without doing any complex calculations.

T.

By on 1/28/2009 3:14 AM ()

The syntax with "member" is slightly different thing. Member with no arguments is compiled as a .NET property and .NET properties are evaluated every time you access them (under the hood, there is a method "get" with type unit -> 'a where 'a is the type returned by the property). It is a good practice to keep properties very simple - they should just return a value without doing any complex calculations.

I agree with you, the "static member" is worst choice from both design and functional perspectives. But for example if TS designs DLS language or something like that this solution could be moderable... Any way, I agree that previous solution is better...

By on 1/28/2009 4:36 AM ()

First you need to put an () after hellow_world_func to let F# know that this is a function that has no paramters.

Then when you call it you will need to add () after each call.

By on 1/27/2009 5:25 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper