Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1912

Confusion regarding generic type function definition inside of another function

$
0
0
  let trace = (x:'a, b: string) => {
    Js.logMany([String.make(x), b])
    x
  }
let main = (): unit => {
  Console.log("Hi")

  let trace_err = trace(_, "err")
  trace("ADaS", "FD")->ignore
  trace(23, "FD")->ignore
  trace_err("DSDA")->ignore
  trace_err(314)->ignore
}

main()

So the code above works if I define trace function in global scope.
But if I put it inside the main like this, it will complains.

let main = (): unit => {
  Console.log("Hi")
let trace = (x:'a, b: string) => {
  Js.logMany([String.make(x), b])
  x
}
  trace("ADaS", "FD")->ignore
  trace(23, "FD")->ignore
}

main()

And if I remove the type notation of x here, it will also work inside of main scope

let main = (): unit => {
  Console.log("Hi")
let trace = (x, b: string) => {
  Js.logMany([String.make(x), b])
  x
}
  trace("ADaS", "FD")->ignore
  trace(23, "FD")->ignore
}

main()

Viewing all articles
Browse latest Browse all 1912

Trending Articles