This is a very elementary question about the F# class syntax. Here is a little code to illustrate my problem.
type AClass() as self =
member this.Something = printfn "Hello"
Basically from what I have read the “as self” will create a name to the current instance which can be used in the entire class (like “this” in C# or Java). But “member this.Something” will do the same thing, only that the scope is limited to the method body. I guess I can see when you would use which syntax. The “as self” one can be used if you need it in the constructor or something and you can use the other one if you dont need it in the constructor.
But why do I have to use the “member this.Something” syntax even if I used the “as self” one? Why does it give me an error if I just write “member Something”? What have I missed?
Take care,
Kerr
The scope of
as selfis the whole class, while scope ofthis.Somethingis just an individual method. You don’t often needas selfsince usingthis.Somethingis adequate.Regarding why you need
this.in member declaration, I think it’s a natural choice since in F# classes you often have let bounds and static methods as well. Havingselfas default would cause confusion and misuse.Here is an example using
as selfin MSDN, which is not common IMO: