Ppx to make names starting with _ (for example)

Would it be possible and reasonably simple to have some convention like this in a ppx?

Can you give an example of how this would be used?

It’s a way to avoid duplicating a lot of signatures in the resi file
Instead of jumping around between resi files or filling the code with ugly decorators it would be just a simple dash at the start of the name
I already use this method just a a visual clue until I create a resi file or not…if there where some simple ppx that could at compile time make them private

I’d say “ugly decorators” are an even better visual clue, and they don’t need any extra tooling.

underscore at the start is no different that having a capital letter
It’s one character vs a bunch of ugly ones or a new whole file repeating half of what’s already written.
Regarding tooling…well, you can always put it in the compiler itself
Also the underscore already was a somewhat “compatible” meaning
But I agree, let’s not turn this into PHP but the question is… how simple would it be to make a tool for this?

do you know %%private? This seems not doable in ppx, it has to be done in the type checker

I actually still don’t understand what you mean :slight_smile: Can you show a concrete code example of how it would be used? I mean an actual sample code, pretending that it exists right now. Correctness doesn’t matter, I just want to get an idea of what it is.

Take the example of bs-fetch.
The Fetch.ml is 430 lines and the Fetch.mli is more than half, 286.
Of course it helps to have a .resi to have a peek at the modules interface but if you really want to understand the inner workings if would be nice to have cleaner (no %%private) code with a simpler visual cue.
Of course, Js bindings, where almost everything is a signature, is an extreme case where you get a lot of code duplication and these are common in ReScript?
Also the fact that Rescript already gives names casing meaning wouldn’t make it so far fetched.
And it could work for filenames also.
I’m also developing some feelings for PHP dollar sign prefixes :slight_smile: …not for this case though

Well…that’s the ugly thing I was talking about :stuck_out_tongue:
It’s a pitty ppx can’t handle that and I don’t want to go back to babel :frowning:

Worth noting that %%private reliably breaks editor tooling like types on hover in submodules.

I read all of this and I still don’t understand what you are asking for. So you don’t want to create a resi file. What kind of syntax do you want to use within a .res file that helps you skip the resi file?

Something like this?

external _somePrivateThing: string = "someprivatething"

The declaration above would only be accessible within the module. Is this what you are talking about?

Exactly!! Is that stupid!? :-S

Even this:
let _myPrivateFunction = () => ....

And this:

Low all the way dash :stuck_out_tongue:

Right now the _ prefix is used to explicitly mark a value as unused (otherwise the compiler will yield an unused-value warning).

IMO the language would need some proper public / private semantics and completely drop interface files… but that’s currently unrealistic for several reasons.

How is that supposed to work? How would you define which modules have access to MyPrivateModule? If you don’t want a module to be accessed, you could also do MyPrivateModule.foobar.res.

Right now the _ prefix is used to explicitly mark a value as unused (otherwise the compiler will yield an unused-value warning).

But that’s not a prefix is it?? Isn’t it the whole thing? If it’s unused you can have a single “_” and that’s wardly a prefix…otherwise I’ve been using a lot of unused variables :stuck_out_tongue:

If you don’t want a module to be accessed, you could also do MyPrivateModule.foobar.res
I don’t have a clue on what that does…wait…I got it, “Exotic Module Filenames”.

Hummm…I like MyPrivateModule better…I think
Something about "
" beeing used for unused names that feels right to use for private names… where the user can’t use them :slight_smile:

If you don’t want a module to be accessed, you could also do MyPrivateModule.foobar.res .

This is a pretty ugly hack relying on some lexical conventions.

%%private is designed specifically for such use cases, when you want to expose most of them and hide only a very few utilities.

Worth noting that %%private reliably breaks editor tooling like types on hover in submodules

Then we should fix that


Yeah but you’re already relying on uppercase words…
Another thing is that you also have a visual cue when you use them.
But again…that was just an idea to be used optionally with some tool not to be on the language itself.
I was just wondering if it would be a simple tool

Note most features can not be done in the syntactic level.

I am thinking maybe we can make the %%private more approachable by adopting a convention __ double underscore, so for double underscore started identifiers it is private by default, thoughts?

1 Like

making it configurable would be a can of worms right?
Is there really a underscore prefix already? Isn’t it just the single one for the unused var?
I guess a single one would break a lot of code but then again… it would be optional and also this is the right time to break stuff right?

Might be worth considering some alternatives. Not sure of implementation effort of these.

  1. @private decorator feels consistent with other ReScript code.

  2. private keyword is common, including TypeScript.

  3. #varname is JS syntax for class methods, but might add confusion with poly vars.

Underscore has been a convention in JS for private variables, so there is perhaps a case for it, but I’m personally not sure about making it a built in language feature.

A wild thought, in ReScript ' is a vaild char for identifier name. I wonder for identifier names ending with ', it is private.

For example:

let hello' =  3 // private
let hello = 3 // exported

Does it click with you?

1 Like

There was a previous discussion that discussed some ideas.

A comment from @Maxim

we could indeed add first class syntax support.
What do you think of private let x = "foo" ?

And you made a good point @Hongbo here:

I find private let or let private always more intuitive than open { .. } , the purpose is not to save some characters, it’s that people understand private without any further explanations which already appears in other languages like F# as mentioned above

Using ' is a nice simple syntax but I suspect it’s not intuitive.

It also might cause a surprise if anyone was not familiar with that syntax and used it as an alternative variable name. I understand in Haskell it’s an idiom to use x and x' and x'' meaning related but different variables.