活动模式和成员约束
对于内联函数,可以创建一个约束,例如:
let inline implicit arg =
( ^a : (static member op_Implicit : ^b -> ^a) arg)
要求参数上有给定的运算符或成员。有没有办法根据相似的东西进行匹配?
我想创建一个活动模式,其中传递给与内联函数的约束相匹配的方法的任何参数都会触发该函数,而其他所有内容都会作为某些错误管理的一部分而结束。
For an inline function one could create a constraint like:
let inline implicit arg =
( ^a : (static member op_Implicit : ^b -> ^a) arg)
requiring the given operator or member on the arguments. Is there a way to match based on somthing similar?
I want to create an active pattern where any argument that's passed to the method that matches the constraint of an inlined function as the above triggers that function and everything else ends as part of some error management.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您也可以编写内联活动模式。我以前没用过这个,但我现在尝试了一下,似乎效果很好。下面的
Test
模式可与任何实现返回option
:Test
方法的对象一起使用。 ^R >现在您可以定义一些定义
Test
方法的对象,并使用模式匹配它们:这看起来是一个非常有趣的技术,因为模式匹配现在是目的。
It looks like you can write inline active patterns too. I haven't used this before, but I tried it now and it seems to work just fine. The
Test
pattern below can be used with any object that implementsTest
method that returnsoption< ^R >
:Now you can define some objects that define
Test
method and match them using the pattern:This looks like a very interesting technique, because pattern matching is now a part of the object.
我认为你必须使用反射,例如有一个接受
o:obj
的函数,然后反射o.GetType()
的成员。I think you'd have to use reflection, e.g. Have a function that takes
o:obj
and then reflect overo.GetType()
's members.