8

Regarding F# self-identifier's as in:

type MyClass2 =
   let data = 123

   member whateverYouWant.PrintMessage() =
       printf "MyClass2 with Data %d" data

The F# class documentation says:

Unlike in other .NET languages, you can name the self identifier however you want; you are not restricted to names such as self, Me, or this.

(The answer to the question What are the benefits of such flexible "self-identifiers" in F#? explains the possible usefulness of this.)

My question is, is there perhaps an unofficial standard of what to name the self-identifier? That is, while there may not be a prescriptive convention, is there a descriptive convention of what are F# programmers doing in the wild? this? x? self?

Update

Well looks like this may get closed, but the answer to the other question is hardly an answer as it's just showing multiple options which I'm already aware of. I'm looking for a consensus. Also, that question was asked in 2009 and there might not have been a consensus at that time, while there may be one now.

Also interesting is the book Expert F# 3.0 authored by Don Syme does not use a consistent self-identifier in the examples. Rather it seems to favor single letter self identifiers especially the letter x.

Community
  • 1
  • 1
User
  • 62,498
  • 72
  • 186
  • 247

3 Answers3

7
type MyClass2 as self =
   let data = 123

   let privatePrintMessage() = self.PrintMessage()

   member this.PrintMessage() =
       printf "MyClass2 with Data %d" data

Some people use member x.Name, but that is faulty because x does not explain what it is, and for one of the most used names in F# - the self identifier should be self-explanatory!

Some people also choose to write member __.Name when the self identifier is unnecessary. Those are usually the same people who make sure there are no unused identifiers (easy to do with --warnon:1182). Other people say that this is weird or that it leads to inconsistency - choose what you like.

Ramon Snir
  • 7,520
  • 3
  • 43
  • 61
  • 3
    This is basically the approach I take -- using `__.Foo` when I don't need the identifier, and `this.Foo` when I do. – Jack P. Mar 13 '13 at 14:03
3

There is a couple of options - people often write this or self or x. I think x is probably the least descriptive one, but it is used quite often - perhaps that is a wrong thing! Another good option is to use an identifier that refers to the name of the class (e.g. person or point), because that is quite informative.

type Point(x:float, y:float) =
  member point.X = x
  member point.Y = y

But I agree that it feels a bit redundant when you're not actually using the self-reference. So using __ would make sense. I think it is a bit unfortunate that you cannot use just ordinary (single) _.

So, in summary, there is probably no single recommended standard - but there is a few common patterns.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • I've opened a [UserVoice issue for allowing wildard self-identifiers](http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/3745318-allow-wildcard-self-identifiers). – Daniel Mar 14 '13 at 14:45
  • What do you find yourself using most frequently? – User Mar 15 '13 at 01:41
  • 1
    @User Tomas used to use `x.` few times in his “Real-World FP” book. I've seen both `x.` and `this.` in recent “F# Deep Dives” early draft. – Artem Koshelev Mar 18 '13 at 11:23
1

A different answer (if you prefer it):

No, there is no standard naming convention for self identifiers. You should choose one for each project, and enforce it.

Ramon Snir
  • 7,520
  • 3
  • 43
  • 61