Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't %A text formatting work with internal modules in F#?

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

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 is not specified as internal, the formatting works as expected. Why doesn't the formatting work with internal modules? Does it have to do with reflection? Is there a workaround besides making the module public?

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

like image 445
Ralphie Avatar asked Nov 08 '22 11:11

Ralphie


1 Answers

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

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

You can see that Add 10 was printed. Also, as I accessed the internal module, both the value and the printfn "%A" statement was executed.

This is the structure of the project:

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 maybe you can provide some more information of F# version, and the environment you are running on. It's possibe to override x.ToString() inside your type as well.

Additional comment:
To be honest there might be something with internal modules, as if I override ToString(), the internal module clearly doesn't like it, and terminates with StackOverFlowException. E.g. 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
[||]

like image 82
s952163 Avatar answered Jan 01 '23 20:01

s952163