Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module and class with same name

Tags:

f#

f#-3.1

This is allowed:

type Test = class end

[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Test = begin end

But this not:

[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Test = begin end

type Test = class end

Why?

In the second case, the error is: Duplicate definition of type or module 'Test'.

I'd love to be able to define some public [<Literal>] constants that are required for a type and important for users of the type inside a module with the same name.

like image 514
Nikon the Third Avatar asked Jul 10 '14 19:07

Nikon the Third


1 Answers

You can open the type declaration, close it and re open later, something like this:

type Test = class end

[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Test =
    [<Literal>]
    let myLiteral = "myLiteral"

type Test with
    static member test = ()

I use this trick all the time ;)

like image 152
Gus Avatar answered Sep 18 '22 22:09

Gus