reasonml / reason

Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
http://reasonml.github.io
MIT License
10.13k stars 428 forks source link

Support conditional compilation / static if #1359

Open bsansouci opened 7 years ago

bsansouci commented 7 years ago

Hey everyone :D I saw that Bucklescript has support for condition compilation but it's a preprocessor fed into -pp, which means we can't use it in conjuction with Reason (since refmt is also a preprocessor).

How could we support static if statement like Bucklescript does? It would help the cross platform story a lot in my opinion!

hcarty commented 7 years ago

https://github.com/mcclure/ppx_const may be a good start. It may need to be updated to use omp and friends.

bobzhang commented 7 years ago

note bucklescript support of conditional compilation is baked in, the -pp is a fallback when you use a normal ocaml compiler. ppx is overkill, it would be nice that reason would have it baked in but it is more challenging to the printer

IwanKaramazow commented 7 years ago

Bucklescript solves this very elegantly in lexing. I'm not 100% sure how we should/can do this in Reason. Since the printer takes an ast & formats based on the tree, we'll need to augment the ast in some kind of way.

bsansouci commented 7 years ago

@hcarty's suggestion seems like the easiest and sanest for now

bsansouci commented 7 years ago

@IwanKaramazow @jordwalke It seems like refmt doesn't support the syntax for https://github.com/janestreet/ppx_optcomp. Do you know how we'd write that syntax https://github.com/mcclure/ppx_const in Reason? I couldn't find something that refmts.

EDIT:

This works for the first link :) Now gotta have crossplatform ppx-es

[%const
    if true {
      module FastHelpers = FastHelpersNative;
      ()
    } else {
      module FastHelpers = FastHelpersJs;
      ()
    }
  ];
yyc-git commented 6 years ago

we need "Support conditional compilation / static if" in reason which is baked in!

@chenglou could you have a plan to support it?

yyc-git commented 6 years ago

@bsansouci how to use https://github.com/mcclure/ppx_const in reason? could you give me suggestion?

bsansouci commented 6 years ago

@yyc-git Hey, right now the only way to use ppx_const would be to depend on it on opam. You need to run

opam switch 4.02.3+buckle-master
eval `opam config env`

to get the right version of the compiler

then add "ocamlfind-dependencies": ["ppx_const"] to your bsconfig.json

I'm not 100% sure it'll work though, I have not tried it.

EDIT: ping me on discord if you want to chat. I can help better in real time.

yyc-git commented 6 years ago

@bsansouci Thanks for your reply!

bsansouci commented 6 years ago

@yyc-git Hey I just landed a change in bsb-native that should allow you to use matchenv. See https://github.com/Schmavery/reprocessing/blob/master/src/Reprocessing_Hotreload.re for the syntax and all. You just need one file that'll be the one that will have its implementation swapped, and then call include [%matchenv ... like this, and switch to get the different implementation. BSB_BACKEND is a magical value that'll be filed in by bsb-native when compiling to each target :)

yyc-git commented 6 years ago

@bsansouci Thankes a lot.

Is Bsb(js backend) support it? I need it in js backbend anyway.

bsansouci commented 6 years ago

Yes, bsb-native is 100% compatible with bsb. Just small extensions to support compiling to bytecode/native :)

yyc-git commented 6 years ago

@bsansouci Good job~

now I'm using Reason 3, which require bsb 2.0. so does bsb-native support reason3? support bsb 2.0?

bsansouci commented 6 years ago

Yup bsb-Native supports reason 3 :)