Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is RequireQualifiedAccess not working, leading to a compilation error, but not if I use open?

Tags:

f#

I'm having trouble with RequireQualifiedAccess: despite the attribute, a union case is shadowing a type. Strangely, the error only appears if I use qualified access on the enclosing module, not inside it or if I open it:

module Module =
    type [<RequireQualifiedAccess>] Du =
        | SomeCase

    type [<RequireQualifiedAccess>] SuperDu =
        | Du of Du

    let valid = Du.SomeCase // Valid, as expected

let invalid = Module.Du.SomeCase // Not defined?!?

open Module
let validToo = Du.SomeCase // Wait, this is valid again?

In the invalid line, there's first a warning that using .Du without qualified access is deprecated, as if I were referring to SuperDu.Du, then it gives an error that SomeCase is not defined.

I always believed that using open X is equivalent to prefixing X. to all definitions taken from that module. But it obviously isn't...?

What is going on here? Do I have to avoid this kind of name collision, despite RequireQualifiedAccess? Is this a compiler bug?

like image 570
Vandroiy Avatar asked Aug 22 '16 16:08

Vandroiy


1 Answers

This is an issue in the F# compiler, which has already been reported. For more information see:

  • [<RequireQualifiedAccess>] on a DU shadows types in the same module
  • Type inference for a record/class behaves differently when module is open or not
like image 110
Tomas Petricek Avatar answered Nov 10 '22 12:11

Tomas Petricek