Why isn’t% Text formatting working with internal modules in F #?

The following code does not format the string as expected (it just prints the full name of the class):

module internal MyModule.Types type Action = | Add of int | Update of int * string | Delete of string module internal MyModule.Print open MyModule.Types let printStuff (x:Action) = printfn "%A" x 

However, if MyModule.Types not specified as internal , formatting works as expected. Why doesn't formatting work with internal modules? Does this have to do with thinking? Is there a workaround besides having the module open?

I should mention that the modules are in separate .fs files.

+5
source share
1 answer

This works for me, for example, I get the output:

λ. \ SO180207.exe
Add 10
Add 5
Add 5
[||]

You can see Add 10 printed. In addition, when I accessed the internal module, both the value and the printfn "%A" operator printfn "%A" .

This is the project structure:

MyModule.fs file:

 module internal MyModule.Types type Action = | Add of int | Update of int * string | Delete of string let x = Add 5 printfn "%A" x 

Program.fs file:

 module MyModule.Program open MyModule.Types [<EntryPoint>] let main argv = let x = Add 10 printfn "%A" x // prints Add 10 printfn "%A" MyModule.Types.x // prints Add 5 twice printfn "%A" argv 0 // return an integer exit code 

So, perhaps you can provide more information about the F # version and the environment in which you work. It is also possible override x.ToString() inside your type.

Additional comment:
Honestly, there might be something with internal modules, as if I were redefining ToString() , the internal module is clearly not like it, and ends up with a StackOverFlowException . For instance. this works without the internal modifier:

 module MyModule.Types type Action = | Add of int | Update of int * string | Delete of string override x.ToString() = sprintf "%A" (x,x) let x = Add 5 printfn "%A" x printfn "%A" (x.ToString()) |> ignore 

λ. \ SO180207.exe
Add 10
Add 5
"(Add 5, Add 5)"
Add 5
[||]

+2
source

Source: https://habr.com/ru/post/1275206/


All Articles