ts2fable icon indicating copy to clipboard operation
ts2fable copied to clipboard

Compiler can't determine the right overload when mixed with U1, U2, U3, etc.

Open MangelMaxime opened this issue 6 years ago • 3 comments

If the export contains several overload of the same method, and some of the arguments are using U1, U2, U3, etc. then the compiler have a hard time determining the type.

Node.fs.readFileSync:

// This doesn't work
let readFileSync (path : string) =
        Node.fs.readFileSync(!^(!^path))

// This works
let readFileSync (path : string) =
    Node.fs.readFileSync(!^(!^path), Some null)

I didn't find a valid version using readdirSync

MangelMaxime avatar Apr 22 '18 08:04 MangelMaxime

After deleting U2,U3,U4 case Below code also doesn't work

open Fable.Core
type NodeExports =
    abstract readFile: path:string * ?option:string -> string
    abstract readFile: path:string * ?option:int -> int
let exports : NodeExports = jsNative
// This doesn't work
let r = exports.readFile ""

humhei avatar Apr 22 '18 09:04 humhei

Yes, overload in F# can give a hard time to the compiler some times.

For your example, this is working (but it's really nice looking and I don't think it's a good idea to make it the default):

open Fable.Core

type NodeExports =
    abstract readFile: path:string * ?option:string -> string
    abstract readFile: path:string * ?option:int -> int

let exports : NodeExports = jsNative

let r = exports.readFile("", unbox<int> null)

or


open Fable.Core

type NodeExports =
    abstract readFile_string: path:string * ?option:string -> string
    abstract readFile_int: path:string * ?option:int -> int

let exports : NodeExports = jsNative

// This doesn't work
let r = exports.readFile_string ""

MangelMaxime avatar Apr 22 '18 09:04 MangelMaxime

For info, this is working too:

let x = Node.fs.readFileSync(unbox<U2<Node.Fs.PathLike,float>> path, Some null) |> U2.Case1

But I add again a hard time making the compiler happy. But still have no new ideas :'(

MangelMaxime avatar Jul 21 '18 20:07 MangelMaxime