内部表示未隐藏在f#模块中

时间:2018-10-29 10:50:34

标签: module f# internals representation

我正在使用模块做项目,在其中计算图像并将其打印在屏幕上

我的代码中的所有内容都可以正常工作,但对此不便之处我并不满意:在我的.fs文件(定义类型和所有函数的文件)中,我声明了

type Picture = P of (Set<((float*float)*(float*float))> * (int*int))

集合描述图片中各段的点(线条) 并且需要一对int来定义显示图像的边框(宽度和高度)

当我尝试使用函数(在另一个文件中)时,我会使用网格函数

let rec setBuilder (ls:((int*int)*(int*int)) list) = 
match ls with
| [] ->  Set.empty
| ((x,y),(w,z))::xs -> setBuilder xs |> Set.add ((float x,float y),(float w, float z)) 

let grid ls (wdt,hgt) = P (setBuilder ls, (wdt, hgt)) 

以此为基础,我创建了一个情侣/情侣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) 

1 个答案:

答案 0 :(得分:2)

只需编写type Picture = private P of ...,其他模块就看不到Picture的内部。

注意:如果编写type private Picture = P of ...,则意味着其他模块看不到Picture类型。