F#:使用和不使用括号定义的类型之间的差异

Ale*_*nko 4 f#

F#type Something()type SomethingF#有什么区别?

为什么ASP.NET Core 1.0 F#项目中引用的此片段有效:

open System
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http

type Startup() = 
    member this.Configure(app: IApplicationBuilder) =
      app.Run(fun context -> context.Response.WriteAsync("Hello from ASP.NET Core!"))

[<EntryPoint>]
let main argv = 
    let host = WebHostBuilder().UseKestrel().UseStartup<Startup>().Build()
    host.Run()
    printfn "Server finished!"
    0
Run Code Online (Sandbox Code Playgroud)

但这失败了:

open System
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http

type Startup = 
    member this.Configure(app: IApplicationBuilder) =
      app.Run(fun context -> context.Response.WriteAsync("Hello from ASP.NET Core!"))

[<EntryPoint>]
let main argv = 
    let host = WebHostBuilder().UseKestrel().UseStartup<Startup>().Build()
    host.Run()
    printfn "Server finished!"
    0
Run Code Online (Sandbox Code Playgroud)

asi*_*ahi 7

您可以通过在F#Interactive中键入它来查看差异:

type Parens() =
    member this.add10 x = x + 10

type NoParens =
    member this.add10 x = x + 10;;
Run Code Online (Sandbox Code Playgroud)

输出:

type Parens =
  class
    new : unit -> Parens
    member add10 : x:int -> int
  end
type NoParens =
  class
    member add10 : x:int -> int
  end
Run Code Online (Sandbox Code Playgroud)

第二个类没有定义构造函数.编译器不应该允许的东西,但出于某种原因.它不会生成像C#这样的自动构造函数.

有关更多信息,请查看F#以获取有关接口的有趣和利润页面.另请查看此StackOverflow帖子

并且为了将来参考,当有疑问时,打开F#interactive,键入你想要看到的两个东西,并比较输出.它是一个强大的工具,你应该使用它.