2

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?

namesis
  • 157
  • 10

3 Answers3

3

I like this version as well:

echo(. ignore())

that way I don't need the dummy value workaround

Edit: This version is also now part of the docs: https://rescript-lang.org/docs/manual/latest/function#uncurried-function

ryyppy
  • 442
  • 3
  • 10
1

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 */

masoodahm
  • 195
  • 1
  • 10
  • 1
    I don't think that calling a function with unit is the same as calling the function without arguments: `()` is the only literal of the `unit` type and I would expect `()` to be a valid argument for a function with signature `'a -> 'a`. For example, in OCaml, I can write either of the following functions: `let f a = a` and `let g(a) = a` and I can call them with the `()` literal. Both functions will return `unit`. Indeed, even in rescript, I can define a function `let f (a) => a;` and `f(())` will return a unit. The question is why can I not do the same when the fucntion is uncurried – namesis Dec 31 '20 at 15:37
0

Apparently, this is a known issue in rescript. See: https://github.com/rescript-lang/rescript-compiler/issues/3429

namesis
  • 157
  • 10