f# - 如何在 F# 中实现访问者模式而不局限于一个文件?
问题描述
以下代码示例演示了访问者模式在 F# 中的实现
module VisitorPattern
type IVisitor =
abstract Visit : ObjectA -> unit
abstract Visit : ObjectB -> unit
and IVisitable =
abstract InvokeVisit : IVisitor -> unit
and ObjectA =
interface IVisitable with
member this.InvokeVisit (visitor: IVisitor) =
visitor.Visit(this)
and ObjectB =
interface IVisitable with
member this.InvokeVisit (visitor: IVisitor) =
visitor.Visit(this)
type MyVisitor =
member this.Visit (a : ObjectA) =
printfn "Visited object A"
member this.Visit (b : ObjectB) =
printfn "Visited object B"
IVisitable
这编译得很好,但是由于使用了and
关键字,我们被限制在一个文件中实现所有类型。这个关键字似乎是允许相互类型引用所必需的。
有没有办法以不局限于一个文件的方式实现这种模式?
(我不是在征求你是否应该在 F# 中使用这种模式的意见)
编辑:我问这个问题是因为访问者模式在与 C# 代码进行互操作时是相关的。
解决方案
模式匹配应该以复杂性和开销的一小部分来实现相同的目标。以我个人的经验,这是在 F# 中实现访问者模式的最佳方式。
type Visitor = A of int | B of int
match a with
| A x -> 1 * x
| B x -> 2 * x
然后对于一些可能的 C#
private static void Main()
{
Visitor a = Visitor.A(7)
switch(a){
case Visitor.A x:
x.Item * 1;
break;
case Visitor.B x:
x.Item * 2;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
推荐阅读
- c++ - C++20 不支持 .mm 文件中的语义导入
- rest - MvC中spring security中每个路径/路由的不同AuthenticationManager
- javascript - 如何使用 vuejs 为 v-radio-button 的每个项目添加字幕?
- python - 在 aws lambda 函数中使用外部 python 库
- python - 没有名为 pafy 的模块
- wordpress - WordPress重定向URL中匹配的字符串
- android - 为什么添加“assets_audio_player”后“flutter run”会产生错误?
- flutter - 如何用圆形磨砂玻璃做到这一点
- python - 考虑新列时如何获取受影响的新索引的数量(计数)
- javascript - Javascript - 根据特定对象选择对象数组中的下一个对象