leostera / caramel

:candy: a functional language for building type-safe, scalable, and maintainable applications
https://caramel.run
Apache License 2.0
1.06k stars 25 forks source link

Atom/Variant which is just underscore is a Syntax Error #97

Open AeroNotix opened 3 years ago

AeroNotix commented 3 years ago

Hi,

I'm trying to convert some code which uses the underscore atom: '_', in this case I'm trying to convert some cowboyrouter code. It uses `''` to mean match any host.

I understand that in OCaml a variant cannot be just named underscore:

; ocaml
        OCaml version 4.11.1

# `_;;
Error: Syntax error

Is there a workaround? There is a pretty well-established convention in Erlang code to use the underscore atom to generally mean 'match anything'. See, cowboy_router and ets matching, for example.

I know I could try, for example, binary_to_atom but that feels quite odd.

xandkar commented 3 years ago

Dunno about Caramel per se, but this works:

$ ocaml
        OCaml version 4.09.1

# `_;;
Error: Syntax error
#
  `__;;
- : [> `__ ] = `__
#
AeroNotix commented 3 years ago

@xandkar hello there :)

I'm really not versed in how the OCaml compiler is being extended or used with Caramel. Is adding an "exception" for this common Erlang use-case of arbitrary atoms to map to Variants feasible?