Fix: Improve series type handling and coercions
This commit enhances the series type handling in `src/ast/rtl/series.rs` by introducing helper methods `as_float` and `as_int` in `src/ast/types.rs` to manage type conversions more robustly. It also updates the function overload resolution in `src/ast/types.rs` to prioritize exact parameter matches before falling back to implicit coercions. Finally, the example `err.myc` has been updated to reflect a more accurate implementation of a Simple Moving Average (SMA) calculation.
This commit is contained in:
+25
-1
@@ -394,6 +394,12 @@ impl StaticType {
|
||||
}
|
||||
|
||||
match (self, other) {
|
||||
// Implicit Coercions
|
||||
(StaticType::Float, StaticType::Int) => true,
|
||||
(StaticType::Int, StaticType::Bool) => true,
|
||||
(StaticType::Int, StaticType::DateTime) => true,
|
||||
(StaticType::DateTime, StaticType::Int) => true,
|
||||
|
||||
// Optional(T) is assignable from T or Void
|
||||
(StaticType::Optional(inner), other) => {
|
||||
matches!(other, StaticType::Void) || inner.is_assignable_from(other)
|
||||
@@ -471,7 +477,8 @@ impl StaticType {
|
||||
}
|
||||
StaticType::FunctionOverloads(sigs) => sigs
|
||||
.iter()
|
||||
.find(|sig| sig.params.is_assignable_from(args_ty))
|
||||
.find(|sig| sig.params == *args_ty) // 1. Try exact match first
|
||||
.or_else(|| sigs.iter().find(|sig| sig.params.is_assignable_from(args_ty))) // 2. Fallback to implicit coercion
|
||||
.map(|sig| sig.ret.clone()),
|
||||
_ => None,
|
||||
}
|
||||
@@ -490,6 +497,23 @@ impl StaticType {
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn as_float(&self) -> Option<f64> {
|
||||
match self {
|
||||
Value::Float(f) => Some(*f),
|
||||
Value::Int(i) => Some(*i as f64),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_int(&self) -> Option<i64> {
|
||||
match self {
|
||||
Value::Int(i) => Some(*i),
|
||||
Value::DateTime(d) => Some(*d),
|
||||
Value::Bool(b) => Some(if *b { 1 } else { 0 }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_truthy(&self) -> bool {
|
||||
match self {
|
||||
Value::Void => false,
|
||||
|
||||
Reference in New Issue
Block a user