Name Tuples/Anonymous Types in F#?

后端 未结 6 1710
执笔经年
执笔经年 2021-02-03 19:46

in C# you can do stuff like :

var a = new {name = \"cow\", sound = \"moooo\", omg = \"wtfbbq\"};

and in

6条回答
  •  半阙折子戏
    2021-02-03 20:29

    I find it easier to do

    let route = routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}" // URL with parameters
        )
    route.Defaults.Add("controller", "Home")
    route.Defaults.Add("action", "Index")
    

    or

    [ "controller", "Home"
      "action", "Index" ]
    |> List.iter route.Defaults.Add
    

    In F#, I would avoid calling overloads that accept anonymous types much as I would avoid calling an F# method accepting FSharpList from C#. Those are language-specific features. Usually there is a language-agnostic overload/workaround available.

    EDIT

    Just looked at the docs--here's yet another way to do it

    let inline (=>) a b = a, box b
    
    let defaults = dict [
      "controller" => "Home"
      "action"     => "Index" 
    ]
    route.Defaults <- RouteValueDictionary(defaults)
    

提交回复
热议问题