简体   繁体   English

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

[英]internal representation not hidden in f# module

I'm doing a project, using modules, where I compute images and print them on screen 我正在使用模块做项目,在其中计算图像并将其打印在屏幕上

Everything in my code work just fine, but I'm not quite happy with this inconvenient: in my .fs file (where I define the type and all my functions) I've declared 我代码中的所有内容都可以正常工作,但是我对此不便感到不满意:在我的.fs文件(我在其中定义类型和所有函数)中声明了

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

Sets describe the points of the segments in the picture (the lines) and the couple of int is needed to define the bounding box where the image will be shown (width and height) 集合描述图片中各段的点(线),需要一对int来定义显示图像的边框(宽度和高度)

When I try my function (in another file) I use my grid function 当我尝试我的函数(在另一个文件中)时,我使用了网格函数

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)) 

With this I take a couple/couple int list and build my Picture 有了这个,我拍了一张情侣/情侣int列表并建立我的照片

The problem is, when I build a Picture with grid (in another file where I try all my function) is visible the internal representation 问题是,当我用网格构建图片(在尝试所有功能的另一个文件中)时,内部表示可见

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

when I interpret the last line I get from the console 当我解释从控制台获得的最后一行时

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))

there is a way to hide this information, I looked up and the solution seems to be the tagged value (but I'm already using them) 有一种隐藏此信息的方法,我抬头看,解决方案似乎是标记的值(但我已经在使用它们了)

* EDIT * *编辑*

I noticed that this behavior could be associated with the static member in my implementation files, without them the inner type is not shown 我注意到此行为可能与实现文件中的静态成员相关联,如果没有它们,则不会显示内部类型

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) 

Simply write type Picture = private P of ... then other modules cannot see the internal of Picture . 简单地写type Picture = private P of ...然后其他模块就看不到Picture的内部。

Note: if you write type private Picture = P of ... it means that other modules cannot see the Picture type. 注意:如果您编写的type private Picture = P of ...则意味着其他模块看不到Picture类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM