Arbitrary order of functions (`fn` / `function` keyword)

I miss the dedicated keyword for function definition, which allows me to be independent of function order. I think that top-bottom functions definition order is better for readability, but it is impossible to achieve in ReScript.

One way to implement this is to use modules (inside file or in different files). But why ReScript doesn’t have fn keyword for this?

I’m not sure it has anything to do with the syntax, rather ReScript simply doesn’t have hoisting. Dunno if it’s easy to add (probably not), how it would affect the compiler performance or whether it would break expectations (e.g., shadowing).

1 Like

There is a way to structure ReScript code with auxiliary functions below the “main” function, by using the rec and and keywords.

let rec myMainFn = () => {

and myFirstFn = () => ()
and mySecondFn = () => ()
and myThirdFn = () => ()

but it is actually abusing the recursive keyword for non-recursive things.

IMO it is just a thing you get used to and at some point you may even prefer it if you know that used functions are declared above (or of course in other modules that are openend).


FWIW, I was annoyed by the lack of hoisting for a while too, but now I prefer it. Incidentally, I’ve also found it fairly common practice in the TS/JS codebases I’ve worked with to apply a similar rule with ESLint (no-use-before-define - ESLint - Pluggable JavaScript Linter).

As already mentioned, for cases when you need recursive functions the rec with and is available!


Thanks! So I guess it is better to get used to, instead of hacking with rec.

1 Like