Module f#模块中未隐藏内部表示
我正在做一个项目,使用模块,计算图像并将其打印到屏幕上 我的代码中的所有内容都工作得很好,但我对这个不方便的地方不太满意:在我的.fs文件(我定义了类型和所有函数)中,我声明了Module f#模块中未隐藏内部表示,module,f#,internals,representation,Module,F#,Internals,Representation,我正在做一个项目,使用模块,计算图像并将其打印到屏幕上 我的代码中的所有内容都工作得很好,但我对这个不方便的地方不太满意:在我的.fs文件(我定义了类型和所有函数)中,我声明了 type Picture = P of (Set<((float*float)*(float*float))> * (int*int)) 有了这个,我得到了一个夫妇/夫妇int列表,并建立了我的图片 问题是,当我构建一个带有网格的图片时(在另一个文件中,我尝试了我所有的函数),内部表示是可见的 let pe
type Picture = P of (Set<((float*float)*(float*float))> * (int*int))
有了这个,我得到了一个夫妇/夫妇int列表,并建立了我的图片
问题是,当我构建一个带有网格的图片时(在另一个文件中,我尝试了我所有的函数),内部表示是可见的
let persn = [((4, 0),(6, 7)); ((6, 7), (6, 10)); ((6, 10), (0, 10)); ((0, 10), (0, 12)); ((0, 12), (6, 12)); ((6, 12), (6, 14)); ((6, 14), (4, 16)); ((4, 16), (4, 18)); ((4, 18), (6, 20)); ((6, 20), (8, 20));
((8, 20), (10,18)); ((10, 18), (10, 16)); ((10, 16), (8, 14)); ((8, 14), (8, 12)); ((8, 12), (10, 12)); ((10, 12), (10, 14)); ((10, 14), (12, 14)); ((12, 14), (12, 10)); ((12, 10), (8, 10)); ((8, 10), (8, 8));
((8, 8), (10, 0)); ((10, 0), (8, 0)); ((8, 0), (7, 4)); ((7, 4), (6, 0)); ((6, 0), (4, 0))]
let box = (15,20)
let person = grid persn box
当我解释从控制台得到的最后一行时
val person : Picture =
P (set
[((0.0, 10.0), (0.0, 12.0)); ((0.0, 12.0), (6.0, 12.0));
((4.0, 0.0), (6.0, 7.0)); ((4.0, 16.0), (4.0, 18.0));
((4.0, 18.0), (6.0, 20.0)); ((6.0, 0.0), (4.0, 0.0));
((6.0, 7.0), (6.0, 10.0)); ((6.0, 10.0), (0.0, 10.0));
((6.0, 12.0), (6.0, 14.0)); ...], (15, 20))
有一种方法可以隐藏这些信息,我查了一下,解决方案似乎就是标记的值(但我已经在使用它们了)
*编辑*
我注意到,这种行为可能与我的实现文件中的静态成员相关联,如果没有它们,则不会显示内部类型
type Picture with
static member(*) (c:float,pic:Picture) =
match pic with
| P(set,(wdt,hgt)) -> P (Set.map (fun ((x,y),(w,z)) -> ((x*c,y*c),(w*c,z*c))) set, (int (round (float wdt * c)) ,int (round (float hgt * c))))
static member(|>>) (pic1:Picture,pic2:Picture) =
match pic1,pic2 with
(P (set1,(w1,h1)), P (set2,(w2,h2))) -> let new_p2 = (((float h1/ float h2)) * pic2)
match new_p2 with
P (nset2,(nw2,nh2)) -> P(Set.union set1 (Set.map (fun ((x,y),(w,z)) -> ((x + (float w1) ,y),(w + (float w1), z)) ) nset2),(w1 + nw2, h1))
static member(|^^) (pic1:Picture,pic2:Picture) =
match pic1,pic2 with
(P (set1,(w1,h1)), P (set2,(w2,h2))) -> let new_pic2 = (((float w1/ float w2)) * pic2)
match new_pic2 with
P (nset2,(nw2,nh2)) -> P(Set.union set1 (Set.map (fun ((x,y),(w,z)) -> ((x,(float h1) + y),(w,(float h1) + z)) ) nset2),(w1 , h1 + nh2))
static member (>||>) (n, pic:Picture) =
match n with
| 0 -> pic
| m -> pic |>> ((m-1) >||> pic)
static member (^||^) (n, pic:Picture) =
match n with
| 0 -> pic
| m -> pic |^^ ((m-1) ^||^ pic)
只需编写
类型Picture=private p of…
其他模块就看不到Picture的内部
注意:如果您编写的type private Picture=p of…
意味着其他模块看不到Picture
类型。在函数式编程中,通常不会隐藏数据的内部表示形式。你为什么要把它藏起来?这纯粹是为了使用FSI时的方便,还是因为您希望如何构造程序?如果您希望FSI不显示最后一个表达式的值,您可以始终在后面添加另一个表达式,如()
。调用fsi.exe
时还有一个--quiet
选项,该选项会抑制除显式打印外的所有输出。我希望该类型被隐藏,因为这是练习的一个请求。什么使您认为它没有隐藏?我知道它会显示,因为当我尝试在单独的文件中使用我的函数时(在这种情况下,我是一个不知道如何实现.fsi文件的用户)并使用我的网格函数创建一个图片,在解释器控制台中,它向我显示P是一对(set+(int,int))嗯,这很奇怪。请检查我的演示代码。如果有private
关键字,我们有一个编译器错误,因为我们正在尝试从其他模块访问类型的内部。删除关键字后错误消失。也许你的F#版本太旧了?我会尽快尝试,可能是版本,昨天我更新了ed monodevelop,但在此之前,我正在运行今年3月下载的版本
type Picture with
static member(*) (c:float,pic:Picture) =
match pic with
| P(set,(wdt,hgt)) -> P (Set.map (fun ((x,y),(w,z)) -> ((x*c,y*c),(w*c,z*c))) set, (int (round (float wdt * c)) ,int (round (float hgt * c))))
static member(|>>) (pic1:Picture,pic2:Picture) =
match pic1,pic2 with
(P (set1,(w1,h1)), P (set2,(w2,h2))) -> let new_p2 = (((float h1/ float h2)) * pic2)
match new_p2 with
P (nset2,(nw2,nh2)) -> P(Set.union set1 (Set.map (fun ((x,y),(w,z)) -> ((x + (float w1) ,y),(w + (float w1), z)) ) nset2),(w1 + nw2, h1))
static member(|^^) (pic1:Picture,pic2:Picture) =
match pic1,pic2 with
(P (set1,(w1,h1)), P (set2,(w2,h2))) -> let new_pic2 = (((float w1/ float w2)) * pic2)
match new_pic2 with
P (nset2,(nw2,nh2)) -> P(Set.union set1 (Set.map (fun ((x,y),(w,z)) -> ((x,(float h1) + y),(w,(float h1) + z)) ) nset2),(w1 , h1 + nh2))
static member (>||>) (n, pic:Picture) =
match n with
| 0 -> pic
| m -> pic |>> ((m-1) >||> pic)
static member (^||^) (n, pic:Picture) =
match n with
| 0 -> pic
| m -> pic |^^ ((m-1) ^||^ pic)