hubFS: THE place for F#

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

Why there is two ways to define Class?

Last post 09-07-2008, 17:22 by colder. 4 replies.
Sort Posts: Previous Next
  •  09-05-2008, 22:13 6933

    Why there is two ways to define Class?

    I know two ways to define Class. But when I want to define a Winform. I don't know how to use them.

    The first one is Simples101. It can initialize something when "new" it. It looks longer and not every type can inherit without ()

    The second one is shorter. But it cannot run some method when creating.

    Why there is two ways? What is the different between them? 



    #light

    open System
    open System.Windows.Forms

    type MyForm1 =
        inherit Form
        val button1 :Button
        member t.InitializeComponent() =
            t.Controls.Add t.button1
        new() as t =
            { button1 = new Button() }
            then
                t.InitializeComponent()

    type MyForm2() =
        inherit Form()
        let button2 = new Button()
        member t.InitializeComponent() =
            t.Controls.Add button2

    let form1 = new MyForm1()
    let form2 =
        let form2 = new MyForm2()
        form2.InitializeComponent()
        form2

    [<STAThread>]
    //do Application.Run(form1)
    do Application.Run(form2)

  •  09-06-2008, 6:43 6938 in reply to 6933

    Re: Why there is two ways to define Class?

    Hi, I just answered another question regarding the class syntax, so you may want to check it out here: http://cs.hubfs.net/forums/thread/6936.aspx.

    Inheriting from types without parameterless constructor is possible using the explicit syntax (your MyForm1) as well. You just have to call the base constructor in your constructor (eg. new() = { inherit Form(123); a = 10 })

    In general, the first syntax is there if you need to define some class in the same way as you'd do in C#.  It allows writing a few things that cannot be done using the second syntax and it is useful for example for CodeDOM, but there are really only a few things that cannot be done using the implicit syntax, so the implicit syntax is preferred, unless you really need something special. Your second example can be written more nicely like this:


    type MyForm2() as t = // 't' is 'this' reference
        inherit Form()
        let button2 = new Button()   
        // local function declaration (not visible from outside of the class)
        let foo () =
          10    
        // initialization in the 'constructor'
        do
          t.Controls.Add button2
        member x.Foo () = foo () // Usual public member - can use local functions
       
    let form2 = new MyForm2()
    form2.Show()


    Tomas Petricek (Blog), C# MVP
    My book: Real-world Functional Programming in .NET
  •  09-06-2008, 7:45 6939 in reply to 6938

    Re: Why there is two ways to define Class?


    You
    could also use the new keyword base so:

    type MyForm2() =
        inherit Form()
        let button2 = new Button()
        do base.Controls.Add button2


    regards,
    Danny

  •  09-06-2008, 12:05 6942 in reply to 6939

    • Art Scott is not online. Last active: 11-11-2008, 20:03 Art Scott
    • Top 500 Contributor
    • Joined on 07-19-2008
    • Menlo Park, CA. Verdant Spaceship Earth
    • Posts 4

    Re: Why there is two ways to define Class?

    Danny where is the new keyword base documented?
    http://research.microsoft.com/fsharp/manual/advanced.aspx#AccessingBaseClasses?

    Thanks, art


    Peace, Love and Happiness
  •  09-07-2008, 17:22 6961 in reply to 6938

    Re: Why there is two ways to define Class?

    Thanks

    Because when I learn F# this year. I have not learned C# yet (and the other OO language). So I don't know how to play with Class before.

    I know how to do something in implicit syntax

    type MyClass() =
    let _ = print_any 1
    do print_any 2

    But I don't know how to access itself (as xxx || base.xxx())

    Thanks all of you! I learn so many new ways to play with Class.
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems