New range variant.

This commit is contained in:
Stephen Chung 2022-07-17 12:09:19 +08:00
parent 0555069de0
commit a12401a1fe
2 changed files with 31 additions and 1 deletions

View File

@ -16,6 +16,7 @@ Enhancements
* `switch` cases can now include multiple values separated by `|`. * `switch` cases can now include multiple values separated by `|`.
* `EvalContext::eval_expression_tree_raw` and `Expression::eval_with_context_raw` are added to allow for not rewinding the `Scope` at the end of a statements block. * `EvalContext::eval_expression_tree_raw` and `Expression::eval_with_context_raw` are added to allow for not rewinding the `Scope` at the end of a statements block.
* A new `range` function variant that takes an exclusive range with a step.
Version 1.8.0 Version 1.8.0

View File

@ -287,7 +287,7 @@ macro_rules! reg_range {
"/// Return an iterator over the exclusive range of `from..to`, each iteration increasing by `step`.", "/// Return an iterator over the exclusive range of `from..to`, each iteration increasing by `step`.",
"/// The value `to` is never included.", "/// The value `to` is never included.",
"///", "///",
"/// If `from` > `to` and `step` < 0, the iteration goes backwards.", "/// If `from` > `to` and `step` < 0, iteration goes backwards.",
"///", "///",
"/// If `from` > `to` and `step` > 0 or `from` < `to` and `step` < 0, an empty iterator is returned.", "/// If `from` > `to` and `step` > 0 or `from` < `to` and `step` < 0, an empty iterator is returned.",
"///", "///",
@ -305,6 +305,35 @@ macro_rules! reg_range {
"/// }", "/// }",
"/// ```" "/// ```"
]); ]);
let _hash = $lib.set_native_fn($x, |range: std::ops::Range<$y>, step: $y| StepRange::new(range.start, range.end, step, $add));
#[cfg(feature = "metadata")]
$lib.update_fn_metadata_with_comments(_hash, [
concat!("range: Range<", stringify!($y), ">"),
concat!("step: ", stringify!($y)),
concat!("Iterator<Item=", stringify!($y), ">")
], [
"/// Return an iterator over an exclusive range, each iteration increasing by `step`.",
"///",
"/// If `range` is reversed and `step` < 0, iteration goes backwards.",
"///",
"/// Otherwise, if `range` is empty, an empty iterator is returned.",
"///",
"/// # Example",
"///",
"/// ```rhai",
"/// // prints all values from 8 to 17 in steps of 3",
"/// for n in range(8..18, 3) {",
"/// print(n);",
"/// }",
"///",
"/// // prints all values down from 18 to 9 in steps of -3",
"/// for n in range(18..8, -3) {",
"/// print(n);",
"/// }",
"/// ```"
]);
)* )*
}; };
} }