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.
在c# 6.0中,编译器可以通过提供“使用静态
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?
是否必须显式指定静态成员的类型?
1 个解决方案
#1
5
The obvious solution, as Fyodor Soikin points out, is to simply make Profile
a module.
正如费奥多尔•索伊金(Fyodor Soikin)所指出的,显而易见的解决方案是将概要文件设置为一个模块。
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.
顺便说一句,虽然目前还不可能在f#中打开静态类,但在未来的版本中,这个特性看起来将会发生变化——这个特性已经“原则上得到了批准”。更多信息请参见用户语音项目。
#1
5
The obvious solution, as Fyodor Soikin points out, is to simply make Profile
a module.
正如费奥多尔•索伊金(Fyodor Soikin)所指出的,显而易见的解决方案是将概要文件设置为一个模块。
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.
顺便说一句,虽然目前还不可能在f#中打开静态类,但在未来的版本中,这个特性看起来将会发生变化——这个特性已经“原则上得到了批准”。更多信息请参见用户语音项目。