1
0

Improve function Display implementation

This commit is contained in:
Jeff 2023-12-30 10:23:00 -05:00
parent 9d94cb9af4
commit 02b30d3730

View File

@ -37,13 +37,13 @@ impl Function {
&self.r#type &self.r#type
} }
pub fn return_type(&self) -> Result<&Type> { pub fn return_type(&self) -> &Type {
match &self.r#type { match &self.r#type {
Type::Function { Type::Function {
parameter_types: _, parameter_types: _,
return_type, return_type,
} => Ok(return_type.as_ref()), } => return_type.as_ref(),
_ => todo!(), _ => &Type::None,
} }
} }
@ -65,10 +65,31 @@ impl Function {
impl Display for Function { impl Display for Function {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!( write!(f, "(")?;
f,
"Function {{ parameters: {:?}, body: {:?} }}", let (parameter_types, return_type) = if let Type::Function {
self.parameters, self.body parameter_types,
) return_type,
} = &self.r#type
{
(parameter_types, return_type)
} else {
return Err(fmt::Error);
};
for (index, (parameter, r#type)) in self
.parameters
.iter()
.zip(parameter_types.iter())
.enumerate()
{
write!(f, "{} <{}>", parameter.inner(), r#type)?;
if index != self.parameters.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, ") -> {}", return_type)
} }
} }