Fable: When generic type and module have the same name module is not created

Created on 14 Nov 2017  路  4Comments  路  Source: fable-compiler/Fable

Description

When generic type and module have the same name module is not created

Repro code

Tried this in the REPL:

type M<'a> = X

module M =
  let Hello = "Hello"

printfn "value: %A" M.Hello

produces this output

value: undefined

Expected and actual results

This non-generic version works:

type M = X

module M =
  let Hello = "Hello"

printfn "value: %A" M.Hello

produces this output

value: "Hello"

Related information

REPL

Most helpful comment

It's intended!
I wouldn't have expected that because I thought the pattern of having a generic type and a module with the same name was a basic one in F# since its used for most types like List, Array, Seq, Option... etc.

Anyway, I can live with the attribute, thank you!

All 4 comments

@amieres Thanks for the report! Yes, this is intended. Not sure if it was the best design decision, but I didn't add a suffix to generic types because I wanted them to be easily accesible from JS. It seems the error message is not happening in the REPL, we probably need to add it, but when using the CLI you should see this:

error FABLE: Public types, modules or functions with same name at same level are not supported: M 

Hint: you can avoid the problem by adding an attribute:

type M<'a> = X

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module M =
  let Hello = "Hello"

It's tricky because in the non-generic version the F# compiler does it automatically, but it doesn't in the generic version because the suffix (erased by Fable) is supposed to be enough to tell the names apart.

It's intended!
I wouldn't have expected that because I thought the pattern of having a generic type and a module with the same name was a basic one in F# since its used for most types like List, Array, Seq, Option... etc.

Anyway, I can live with the attribute, thank you!

I'm working on https://github.com/fable-compiler/ts2fable/issues/105 and adding the attribute is not working for me. When compiling Node.fs I get:

fable: Compiled src\Node.fs
C:/Users/camer/fs/ts2fable/src/Node.fs(1,1): error FABLE: Public namespaces, modules, types or functions with same name at same level are not supported: os
C:/Users/camer/fs/ts2fable/src/Node.fs(1,1): error FABLE: Public namespaces, modules, types or functions with same name at same level are not supported: net
C:/Users/camer/fs/ts2fable/src/Node.fs(1,1): error FABLE: Public namespaces, modules, types or functions with same name at same level are not supported: fs

The code is this:

    type PathLike =
        U3<string, Buffer, URL>

    [<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
    module PathLike =
        let ofString v: PathLike = v |> U3.Case1
        let isString (v: PathLike) = match v with U3.Case1 _ -> true | _ -> false
        let asString (v: PathLike) = match v with U3.Case1 o -> Some o | _ -> None
        let ofBuffer v: PathLike = v |> U3.Case2
        let isBuffer (v: PathLike) = match v with U3.Case2 _ -> true | _ -> false
        let asBuffer (v: PathLike) = match v with U3.Case2 o -> Some o | _ -> None
        let ofURL v: PathLike = v |> U3.Case3
        let isURL (v: PathLike) = match v with U3.Case3 _ -> true | _ -> false
        let asURL (v: PathLike) = match v with U3.Case3 o -> Some o | _ -> None

Nevermind, my onflict was my NodeExt.fs file.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

et1975 picture et1975  路  43Comments

grishace picture grishace  路  28Comments

tomcl picture tomcl  路  26Comments

alfonsogarciacaro picture alfonsogarciacaro  路  26Comments

chrisvanderpennen picture chrisvanderpennen  路  31Comments