Reputation: 11595
How can I use the compiler to infer the type of a static member?
In C# 6.0, the compiler can infer the type of a static property or method by providing the "using static <namespace>.<class>"
directive.
Thus, I want to write as little code as possible.
I have the following static members:
module Mock
type Profile() =
static member SomeUserName = "some_user_name"
static member SomePassword = "some_password"
I then define some types and a function:
open Mock
(*Types*)
type User = User of string
type Password = Password of string
type Credentials = { User:User; Password:Password }
(*Functions*)
let login credentials =
false
I then have the following test:
[<Test>]
let ``sign in`` () =
// Setup
let credentials = { User= User Profile.SomeUserName
Password= Password Profile.SomePassword }
// Test
credentials |> login
|> should equal true
I would like to remove the Profile type qualifier from SomeUserName and SomePassword and do this instead:
// Setup
let credentials = { User= User SomeUserName
Password= Password SomePassword }
Do I have to explicitly specify a type of a static member?
Upvotes: 1
Views: 71
Reputation: 12184
The obvious solution, as Fyodor Soikin points out, is to simply make Profile
a module.
module Profile =
let someUserName = "some_user_name"
let somePassword = "some_password"
Then you can just:
open Profile
let credentials = {
User = User someUserName;
Password = Password somePassword
}
Incidentally, while it may not be possible to open static classes in F# at the moment, this looks set to change in future versions - this feature has been "approved in principle". See the user voice item for more.
Upvotes: 5