Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# instance syntax

Tags:

f#

What indicator do you use for member declaration in F#? I prefer

member a.MethodName

this is to many letters and x is used otherwise.

like image 813
napoleonss Avatar asked Oct 12 '10 08:10

napoleonss


4 Answers

I do almost always use x as the name of this instance. There is no logic behind that, aside from the fact that it is shorter than other options.

The options that I've seen are:

member x.Foo     // Simply use (short) 'x' everywhere
member ls.Foo    // Based on type name as Benjol explains
member this.Foo  // Probably comfortable for C# developers
member self.Foo  // I'm not quite sure where this comes from!
member __.Foo    // Double underscore to resemble 'ignore pattern' 
                 // (patterns are not allowed here, but '__' is identifier)

The option based on the type name makes some sense (and is good when you're nesting object expressions inside a type), but I think it could be quite difficult to find reasonable two/three abbreviation for every type name.

like image 52
Tomas Petricek Avatar answered Nov 15 '22 04:11

Tomas Petricek


Don't have a system. Wonder if I should have one, and I am sure there will be a paradigm with its own book some day soon. I tend to use first letter(s) of the type name, like Benjol.

This is a degree of freedom in F# we could clearly do without. :)

like image 29
Alexander Rautenberg Avatar answered Nov 15 '22 05:11

Alexander Rautenberg


I tend to use some kind of initials which represent the type so:

type LaserSimulator = 
    member ls.Fire() = 
like image 34
Benjol Avatar answered Nov 15 '22 03:11

Benjol


I largely tend to use self.MethodName, for the single reason that self represents the current instance by convention in the other language I use most: Python. Come to think of it, I used Delphi for some time and they have self as well instead of this.

I have been trying to convert to a x.MethodName style, similar to the two books I am learning from: Real World Functional Programming and Expert F#. So far I am not succeeding, mainly because referring to x rather than self (or this) in the body of the method still confuses me.

I guess what I am saying is that there should be a meaningful convention. Using this or self has already been standardised by other languages. And I personally don't find the three letter economy to be that useful.

like image 44
Muhammad Alkarouri Avatar answered Nov 15 '22 04:11

Muhammad Alkarouri