use std::error::Error;
type LooseResult<T> = Result<T, Box<dyn Error>>;
#[derive(Debug)]
enum ValueKind {
Null,
Int(i64),
Decimal(f64),
Bool(bool)
}
struct Value {
value: ValueKind
}
impl Value {
pub fn new(value: ValueKind) -> Self {
return Value { value }
}
pub fn to_string(&self) -> Result<String, String> {
match self.value {
ValueKind::Null => Ok(String::from("null")),
ValueKind::Int(v) => Ok(String::from(v.to_string())),
ValueKind::Bool(v) => Ok(String::from(v.to_string())),
ValueKind::Decimal(v) => Ok(String::from(v.to_string())),
}
}
pub fn to_int(&self) -> Result<i64, String> {
match self.value {
ValueKind::Int(v) => Ok(v),
ValueKind::Decimal(v) => Ok(v as i64),
_ => Err(String::from(format!("unable to convert type of {:?} to integer.", self.value)))
}
}
pub fn to_decimal(&self) -> Result<f64, String> {
match self.value {
ValueKind::Int(v) => Ok(v as f64),
ValueKind::Decimal(v) => Ok(v),
_ => Err(String::from(format!("unable to convert type of {:?} to decimal.", self.value)))
}
}
pub fn add_to(&self, other: &Value) -> Result<Value, String> {
match self.value {
ValueKind::Int(v) => Ok(Value::new(ValueKind::Int(v + other.to_int()?))),
ValueKind::Decimal(v) => Ok(Value::new(ValueKind::Decimal(v + other.to_decimal()?))),
_ => Err(String::from(format!("unable to add {:?} to {:?}!", self.value, other.value)))
}
}
}
fn main() {
let value1 = Value::new(ValueKind::Int(123));
let value2 = Value::new(ValueKind::Decimal(3.14));
let value3 = value1.add_to(&value2).expect("failed");
println!("result: {}", value3.to_string().expect("failed"));
} use std::error::Error;
type LooseResult<T> = Result<T, Box<dyn Error>>;
#[derive(Debug)]
enum ValueKind {
Null,
Int(i64),
Decimal(f64),
Bool(bool)
}
struct Value {
value: ValueKind
}
impl Value {
pub fn new(value: ValueKind) -> Self {
return Value { value }
}
pub fn to_string(&self) -> Result<String, String> {
match self.value {
ValueKind::Null => Ok(String::from("null")),
ValueKind::Int(v) => Ok(String::from(v.to_string())),
ValueKind::Bool(v) => Ok(String::from(v.to_string())),
ValueKind::Decimal(v) => Ok(String::from(v.to_string())),
}
}
pub fn to_int(&self) -> Result<i64, String> {
match self.value {
ValueKind::Int(v) => Ok(v),
ValueKind::Decimal(v) => Ok(v as i64),
_ => Err(String::from(format!("unable to convert type of {:?} to integer.", self.value)))
}
}
pub fn to_decimal(&self) -> Result<f64, String> {
match self.value {
ValueKind::Int(v) => Ok(v as f64),
ValueKind::Decimal(v) => Ok(v),
_ => Err(String::from(format!("unable to convert type of {:?} to decimal.", self.value)))
}
}
pub fn add_to(&self, other: &Value) -> Result<Value, String> {
match self.value {
ValueKind::Int(v) => Ok(Value::new(ValueKind::Int(v + other.to_int()?))),
ValueKind::Decimal(v) => Ok(Value::new(ValueKind::Decimal(v + other.to_decimal()?))),
_ => Err(String::from(format!("unable to add {:?} to {:?}!", self.value, other.value)))
}
}
}
fn main() {
let value1 = Value::new(ValueKind::Int(123));
let value2 = Value::new(ValueKind::Decimal(3.14));
let value3 = value1.add_to(&value2).expect("failed");
println!("result: {}", value3.to_string().expect("failed"));
}
I built lots of VMs in C# and zig (although they were personal only) so I want to do one in rust too.
This is my current prototype for heterogeneous values which I use a lot.
If you have any feedback, please enlighten me!