How do you call an uncurried function with unit in ReScript/ReasonML?

Viewed 298

Say that I have an uncurried function like:

let echo(. a) = a;

I can call this funcition fine with most literals like:

echo(. 1)
echo(. "Hello")

but when I am trying to call it with void, I get an error:

echo(. ()) //This function has arity1 but was expected arity0

As a workaround, I can do this:

let myArg = ()
echo(. myArg)

Is there a way to avoid this?

3 Answers

It looks like you are trying to call an un curried function that takes one parameter with no arguments because that is what () means right. The only reason it works with echo(. myArg) is in this case the compiler is declaring an un initialized variable called myArg and passing that to echo. EDIT: you can look at what I mean at re-script playground here

let echo = (. a) => a;


let k =() => echo(. "String")
let myArg = ()
let s =() => echo (. myArg)

generates

// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';


function echo(a) {
  return a;
}

function k(param) {
  return echo("String");
}

function s(param) {
  return echo(undefined);
}

var myArg;

exports.echo = echo;
exports.k = k;
exports.myArg = myArg;
exports.s = s;
/* No side effect */

Related