cascade icon indicating copy to clipboard operation
cascade copied to clipboard

Loops inside cascade! are not explained well

Open TPReal opened this issue 4 years ago • 1 comments

In an example it says:

let hash_map = cascade! {
  HashMap::new();
  ..insert("foo", "bar");
  println!("Look! You can put statements in a cascade!");
  for i in 0..3 {
    println!("You can put loops in here too! Make sure to put a semicolon at the end!");
  };
};

So there's a loop inside cascade!, but it doesn't invoke any ..method. Is it possible to invoke them? Also the remark about semicolon is very vague, what's the rule here? Why should it be there?

My code:

let x = cascade! {
  MultiLinFunc::new_zero(blink_low_brightness);
  ..add_const_segm(t_in_out);
  for i in 0..blink_rep {
    ..add_segm(t_unit, 1.0);
    ..add_segm(t_unit, blink_low_brightness);
  };
  ..add_segm(t_in_out, 0.0);
};

doesn't compile, I get complaints about the two calls inside the loop:

error[E0425]: cannot find function `add_segm` in this scope
  --> src/weather_clock.rs:36:11
   |
36 |         ..add_segm(t_unit, 1.0);
   |           ^^^^^^^^ not found in this scope

error[E0425]: cannot find function `add_segm` in this scope
  --> src/weather_clock.rs:37:11
   |
37 |         ..add_segm(t_unit, blink_low_brightness);
   |           ^^^^^^^^ not found in this scope

So I suppose the crate doc is not clear enough on this. I don't really know if it's supposed to work and I'm just doing something stupid, or it's not expected to work in the first place.

TPReal avatar Dec 08 '20 11:12 TPReal

@TPReal Thank you for opening this! Sorry, I should have made the documentation more clear: it is not possible to put .. within loops. Unfortunately, due to limitations with Rust's macro parser, it is not possible for cascade to parse for/while statements, so it treats all loops as a single expression. Of course, it lets you put .. inside of blocks, so I can see how this would be confusing.

I'll update the documentation to explain this. Sorry for taking so long to get to this issue (I never got a notification for it). Let me know if you have any further questions!

snowsignal avatar Jan 06 '21 04:01 snowsignal