名F#元组/匿名类型? [英] Name Tuples/Anonymous Types in F#?

查看:173
本文介绍了名F#元组/匿名类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,你可以做的东西,如:



<预类=郎-CS prettyprint-覆盖> VAR一个=新{名=牛,声音=moooo,OMG =wtfbbq};

和Python中,你可以做的东西像



<前类=郎-PY prettyprint-覆盖> A = T(NAME =牛,声音=moooo,OMG =wtfbbq)

缺省情况下,当然,但它是微不足道的实现类 T ,让你做到这一点。逸岸我确实做到了,当我使用Python,发现它要能够按名称而不按索引(这是很容易混淆)来访问组件的小容器一次性令人难以置信的方便。



除此之外细节,他们基本上是相同的,使得他们在利基元组。



在特定的,我期待在此C#代码现在:



<预类=郎-CS prettyprint-覆盖> routes.MapRoute(
默认,//路线名称
{控制器} / {行动} / {ID},// URL带参数
新的{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
);

和它的F#相当于



<预类=郎毫升prettyprint-覆盖> 键入路径= {
控制器:字符串
动作:字符串
编号:UrlParameter}

路线。图路线(
默认,//路线名称
{控制器} / {行动} / {ID},// URL带参数
{控制器=家,行动= 指数; ID = UrlParameter.Optional} //参数默认

这是既冗长和重复,更何况相当恼人。您有多近可以得到这种语法在F#?我不介意通过一些箍(甚至燃烧的火圈!)现在,如果这意味着它会给我一些干涸这样的代码很有用跳。


解决方案

我觉得更容易做。

 让路线= routes.MapRoute(
默认,//路线名称
{控制器} / {行动} / {ID}// URL带参数

route.Defaults.Add(控制器,主页)
route.Defaults.Add(行动,索引)

  [控制器,家
行动,索引]
|> ; List.iter route.Defaults.Add

在F#中,我会避​​免调用,许多接受匿名类型重载因为我会避免调用一个F#方法接受 FSharpList 从C#。这些都是特定语言的功能。通常有语言无关的过载/解决方法可用。



修改



只是看着docs--这里是另一种方式来做到这一点。

 让内联(=>)AB = A,b盒

让默认=字典[
控制器=> 家
行动=> 索引
]
route.Defaults< - RouteValueDictionary(默认)


in C# you can do stuff like :

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

and in Python you can do stuff like

a = t(name = "cow", sound = "moooo", omg = "wtfbbq")

Not by default, of course, but it's trivial to implement a class t that lets you do it. Infact I did exactly that when I was working with Python and found it incredibly handy for small throwaway containers where you want to be able to access the components by name rather than by index (which is easy to mix up).

Other than that detail, they are basically identical to tuples in the niche they serve.

In particular, I'm looking at this C# code now:

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

and it's F# equivalent

type Route = { 
    controller : string
    action : string
    id : UrlParameter }

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    { controller = "Home"; action = "Index"; id = UrlParameter.Optional } // Parameter defaults
  )

Which is both verbose and repetitive, not to mention rather annoying. How close can you get to this sort of syntax in F#? I don't mind jumping through some hoops (even flaming hoops!) now if it means it'll give me something useful to DRY up code like this.

解决方案

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)

这篇关于名F#元组/匿名类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆