Allow abool -> bool coercion

The expression `sometimes & true` now evaluates to `true` 50% of the
time and false 50% of the time, rather than throwing a type error
because `sometimes` is not a bool.
This commit is contained in:
Alexander Bethel 2021-05-21 12:25:37 -05:00
parent 464337bd53
commit e79fc9a005

View file

@ -56,10 +56,9 @@ impl TryFrom<Value> for i32 {
type Error = Error; type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> { fn try_from(value: Value) -> Result<Self, Self::Error> {
if let Value::Int(i) = value { match value {
Ok(i) Value::Int(i) => Ok(i),
} else { _ => Err(Error {
Err(Error {
kind: ErrorKind::TypeError(format!("Expected int, got {}", value)), kind: ErrorKind::TypeError(format!("Expected int, got {}", value)),
// TODO: either add some kind of metadata to `Value` // TODO: either add some kind of metadata to `Value`
// so we can tell where the value came from and assign // so we can tell where the value came from and assign
@ -67,7 +66,7 @@ impl TryFrom<Value> for i32 {
// `error::Error` struct so we can omit the `position` // `error::Error` struct so we can omit the `position`
// when using some error kinds. // when using some error kinds.
position: 0..0, position: 0..0,
}) }),
} }
} }
} }
@ -76,13 +75,13 @@ impl TryFrom<Value> for bool {
type Error = Error; type Error = Error;
fn try_from(value: Value) -> Result<Self, Self::Error> { fn try_from(value: Value) -> Result<Self, Self::Error> {
if let Value::Bool(b) = value { match value {
Ok(b) Value::Bool(b) => Ok(b),
} else { Value::Abool(b) => Ok(b.into()),
Err(Error { _ => Err(Error {
kind: ErrorKind::TypeError(format!("Expected bool, got {}", value)), kind: ErrorKind::TypeError(format!("Expected bool, got {}", value)),
position: 0..0, position: 0..0,
}) }),
} }
} }
} }