hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

unit type

Last post 05-23-2007, 21:06 by Robert. 3 replies.
Sort Posts: Previous Next
  •  05-23-2007, 14:15 3157

    unit type

    I have a windows forms button and I attach a function to its Click event, how can I make the function evaluate to the unit type?

    let button = new Button()

    button.Click.Add(buttonClicked)

     

    let buttonClicked (e:System.EventArgs) =

    let d = MessageBox.Show(e.ToString())

    printf ""          // stupid way of forcing the expression to evaluate to the unit type .. (?)

     

    Any suggestions?

  •  05-23-2007, 14:32 3158 in reply to 3157

    Re: unit type

    Hi,

    "()" is used to represent unit.  It's very general and can be used in a number of situations, for example;

    > let x = fun () -> ();;
    val x : unit -> unit
    > x;;
    val it : (unit -> unit) = <fun:clo@0>
    > x();;
    val it : unit = ()


    Cheers,

    Andy

  •  05-23-2007, 14:39 3159 in reply to 3158

    Re: unit type

    thank you for the quick (and correct!) reply :)
  •  05-23-2007, 21:06 3160 in reply to 3159

    Re: unit type

    You probably want to use the ignore function to make something evealuate to the unit type.

    let buttonClicked (e:System.EventArgs) =
        MessageBox.Show(e.ToString()) |> ignore

    I'm probably going to start sounding like a broken record, but this is covered at the beginning of chapter 4 in my book:
    http://www.amazon.com/dp/1590597575?tag=strangelights-20&camp=14573&creative=327641&linkCode=as1&creativeASIN=1590597575&adid=0F4QKB6A95B2Z4NW1BFN


    Robert Pickering, MVP
    http://strangelights.com
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems