As mentioned in scrwtp, this seems to be a commonly used identifier, and this is my preference. Another very common is x . I usually use the class level identifier when it has been used several times throughout the class and, of course, when it was used in the constructor. And in these cases, I would use __ (two underscores) as a member level identifier to indicate that the value is being ignored. You cannot use _ and actually ignore it, since it is a compilation error, but linting tools often treat __ as the same thing and do not give you a warning about an unused identifier.
When you add an identifier at the class level and do not use it, you get a warning:
A recursive reference to the 'self' object is not used. Having a recursive object reference adds runtime initialization checks to members of this and derived types. Try removing this recursive object reference.
Consider this code:
type MyClass() = member self.X = self type MyClassAsSelf() as self = member __.X = self type MyClassAsSelfUnused() as self =
Here's what these classes look like after compilation / decompilation:
public class MyClass { public Program.MyClass X { get { return this; } } public MyClass() : this() { } }
public class MyClassAsSelf { internal FSharpRef<Program.MyClassAsSelf> self = new FSharpRef<Program.MyClassAsSelf>(null); internal int init@22 ; public Program.MyClassAsSelf X { get { if ( this.init@22 < 1) { LanguagePrimitives.IntrinsicFunctions.FailInit(); } return LanguagePrimitives.IntrinsicFunctions.CheckThis<Program.MyClassAsSelf>(this.self.contents); } } public MyClassAsSelf() { FSharpRef<Program.MyClassAsSelf> self = this.self; this..ctor(); this.self.contents = this; this.init@22 = 1; } }
public class MyClassAsSelfUnused { internal int init@25-1 ; public Unit X { get { if ( this.init@25-1 < 1) { LanguagePrimitives.IntrinsicFunctions.FailInit(); } } } public MyClassAsSelfUnused() { FSharpRef<Program.MyClassAsSelfUnused> self = new FSharpRef<Program.MyClassAsSelfUnused>(null); FSharpRef<Program.MyClassAsSelfUnused> self2 = self2; this..ctor(); self.contents = this; this.init@25-1 = 1; } }
Note that there is a check that the variable was set in the constructor. If the test fails, the function is called: LanguagePrimitives.IntrinsicFunctions.FailInit() . This is an exception:
System.InvalidOperationException: Initializing an object or value led to recursive access of the object or object until it was fully initialized.
I assume the warning only exists so that you can avoid the slight overhead of unnecessary runtime checking. However, I do not know how to create a situation in which an error occurs, so I do not know the exact purpose of the check. Perhaps someone else can shed light on this?
TheQuickBrownFox
source share