use crate::types::Type;
use std::fmt::{Display, Formatter, Write};
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Function {
pub params: Vec<Arc<Type>>,
pub return_type: Arc<Type>,
}
impl Function {
pub fn new(params: Vec<Type>, return_type: Type) -> Self {
Self {
params: params.into_iter().map(Arc::new).collect(),
return_type: Arc::new(return_type),
}
}
pub fn fmt_indented(
&self,
f: &mut impl Write,
indentation: usize,
max_fields: usize,
) -> std::fmt::Result {
write!(f, "fn(")?;
for (i, param) in self.params.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
param.fmt_indented(f, indentation, max_fields)?;
}
write!(f, ") -> ")?;
self.return_type.fmt_indented(f, indentation, max_fields)
}
pub fn subfields(&self) -> usize {
self.params.iter().map(|p| p.subfields()).sum::<usize>() + self.return_type.subfields()
}
}
impl Display for Function {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.fmt_indented(f, 0, usize::MAX)
}
}