ISibboI / evalexpr

A powerful expression evaluation crate 🦀.
GNU Affero General Public License v3.0
320 stars 52 forks source link

allow set existing variable with another type #167

Open unidevel opened 8 months ago

unidevel commented 8 months ago

I'm using this to eval expr for a table. I want to set the current value as "$$", but the types of columns are different, when I use set_value from the context, it give error like Err(ExpectedInt { actual: String("test") }) when I try to set a string to an existing variable with value of int.

The test code is:

use evalexpr::{eval_with_context, ContextWithMutableVariables, HashMapContext, Value};

fn main() {
    let mut ctx = HashMapContext::new();
    let r1 = ctx.set_value("$$".into(), Value::from(123));
    let r2 = ctx.set_value("$$".into(), Value::from("test"));
    let r = eval_with_context("$$+1", &ctx);
    println!("result: {:?}, {:?}, {:?}", r1, r2, r);
}

Can you add a function to clear the variable or allow set_value with new type?