pub enum SValue {
Text(ARc<str>),
Int(i64),
Float(f64),
Bool(bool),
}Expand description
This should model a Scratch value.
Scratch treats texts that are non-numeric as the number 0 and also stores numbers
inside of arithmetic expressions as texts, at least sometimes.
So it is useful to have a type that mimics this implicit conversion behaviour.
Variants§
Implementations§
Source§impl SValue
impl SValue
Sourcepub fn q_eq<Q>(&self, other: &SValue, sink: &mut Q) -> bool
pub fn q_eq<Q>(&self, other: &SValue, sink: &mut Q) -> bool
Compares two Scratch values for equality
If any of the two values is a non-numeric text (it can’t be parsed as float or int) or a boolean, both values will be converted to strings. Those strings will then be compared for equality.
Otherwise, the numeric equality is used as in SNumber::q_eq.
Source§impl SValue
impl SValue
Source§impl SValue
impl SValue
Sourcepub const fn default_empty_cmp_hole() -> Self
pub const fn default_empty_cmp_hole() -> Self
For empty wholes in the shape of a comparison block,
Scratch automatically treats its value as false
Sourcepub fn default_empty_expr_hole() -> Self
pub fn default_empty_expr_hole() -> Self
For empty wholes in the shape of an expression block, Scratch automatically treats its value as the empty string
Source§impl SValue
impl SValue
pub fn is_float(&self) -> bool
pub fn is_int(&self) -> bool
pub fn is_number(&self) -> bool
Sourcepub fn is_bool_but_not_as_text(&self) -> bool
pub fn is_bool_but_not_as_text(&self) -> bool
Comparison operations (=, < and >) treat the texts "true" and "false"
for comparisons differently than the boolean literals true and false
that could be generated by <>-shaped-blocks.
(Boolean literals true and false are equal to 1 and 0 while
"true" and "false" force a textual comparison so then they are not
equal to 1 and 0.)
This method is used in cases where it is not allowed to parse the string literals as booleans automatically.
pub fn as_bool(&self) -> bool
pub fn as_text(&self) -> Cow<'_, str>
Source§impl SValue
impl SValue
Sourcepub fn q_as_bool<Q>(&self, sink: &mut Q) -> boolwhere
Q: QuirkSink<SValueToBoolQ>,
pub fn q_as_bool<Q>(&self, sink: &mut Q) -> boolwhere
Q: QuirkSink<SValueToBoolQ>,
Converts the value to a boolean according to Scratch logic.
| input | associated boolean |
|---|---|
"" | false |
"false" | false |
"true" | true |
| Int 0 / Float 0.0 | false |
| Int 1 / Float 1.0 | true |
| other string | true |
| other number | true |
| already boolean | unchanged |
Occuring behaviour differences Scratch implements to avoid errors will be reported to the sink.
As expression blocks can’t be placed in comparison contexts these quriks should never occur because of a user’s program.
For completness they are reported.
Source§impl SValue
impl SValue
pub fn q_as_float<Q>(&self, sink: &mut Q) -> f64where
Q: QuirkSink<SValueToFloatQ>,
Source§impl SValue
impl SValue
pub fn q_as_number_permissive_bool_text<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SValueToNumberQ>,
pub fn q_as_number<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SValueToNumberQ>,
Sourcepub fn q_as_number_strict_bool_text<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SValueToNumberQ>,
pub fn q_as_number_strict_bool_text<Q>(&self, sink: &mut Q) -> SNumberwhere
Q: QuirkSink<SValueToNumberQ>,
Source§impl SValue
impl SValue
Source§impl SValue
impl SValue
pub fn nth_letter_of_me<Q>(&self, one_based_n: &SValue, sink: &mut Q) -> SValue
Source§impl SValue
impl SValue
pub fn concat_no_limit(&self, other: &SValue) -> ARc<str>
Sourcepub fn concat(
&self,
other: &SValue,
max_string_length: usize,
) -> Option<ARc<str>>
pub fn concat( &self, other: &SValue, max_string_length: usize, ) -> Option<ARc<str>>
Concatenates this value (as string) with another value (as string)
Sourcepub fn contains_text(&self, maybe_contained: &SValue) -> bool
pub fn contains_text(&self, maybe_contained: &SValue) -> bool
Does this text contain another text?
Sourcepub fn textual_length(&self) -> usize
pub fn textual_length(&self) -> usize
The number of characters in the textual representation of a value
Trait Implementations§
Auto Trait Implementations§
impl !Send for SValue
impl !Sync for SValue
impl Freeze for SValue
impl RefUnwindSafe for SValue
impl Unpin for SValue
impl UnsafeUnpin for SValue
impl UnwindSafe for SValue
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more