How to use %%raw inside functions

I need to break for-loop.

let doSome = () => {
  for index in 0 to 10 {
  if index == 5 {

Why can’t I use %%raw inside some ReScript code?

%%raw lets you embed top-level raw JS code, %raw lets you embed expression-level JS code:

You need %raw

%raw puts code in brackets and it doesn’t work… :frowning_face:

Oh Yeah. I think it did not work since break is a statement not expression. and %raw expects an expression.

Yes, you are right. I think that re-script should to add possible inserting raw code inside expressions.

This doesn’t directly answer the question, but the “ReScript way” of making a loop you can break out of is to use a recursive function instead of a loop:

let doSome = () => {
  let rec loop = index =>
    switch index {
    | 10 => Js.log("done")
    | 5 => Js.log("breaking")
    | index => loop(index + 1)

If you look at the output of this example, you can see that the compiler optimizes it into a while loop.


WOW! Thanks a lot! It’s nice for me.

Another way to simulate ‘break’ functionality is using a while loop:

let doSome = () => {
  let index = ref(0)
  while index.contents <= 10 && index.contents != 5 {

It will works too. But previous decision is better for me. Thanks.