From bca035172c463e6ac4aaf2591d7d4af2da51c522 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 18 Jun 2021 11:59:05 +0200 Subject: Join semantics --- src/eval/value.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'src/eval/value.rs') diff --git a/src/eval/value.rs b/src/eval/value.rs index f2bc4fd6..b29d01f3 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -5,6 +5,7 @@ use std::fmt::{self, Debug, Display, Formatter}; use std::ops::Deref; use std::rc::Rc; +use super::ops; use super::EvalContext; use crate::color::{Color, RgbaColor}; use crate::exec::ExecContext; @@ -61,14 +62,6 @@ impl Value { Self::Template(vec![TemplateNode::Func(TemplateFunc::new(name, f))]) } - /// Try to cast the value into a specific type. - pub fn cast(self) -> CastResult - where - T: Cast, - { - T::cast(self) - } - /// The name of the stored value's type. pub fn type_name(&self) -> &'static str { match self { @@ -125,6 +118,26 @@ impl Value { _ => None, } } + + /// Try to cast the value into a specific type. + pub fn cast(self) -> CastResult + where + T: Cast, + { + T::cast(self) + } + + /// Join with another value. + pub fn join(self, ctx: &mut EvalContext, other: Self, span: Span) -> Self { + let (lhs, rhs) = (self.type_name(), other.type_name()); + match ops::join(self, other) { + Ok(joined) => joined, + Err(prev) => { + ctx.diag(error!(span, "cannot join {} with {}", lhs, rhs)); + prev + } + } + } } impl Default for Value { -- cgit v1.2.3