aboutsummaryrefslogtreecommitdiff
path: root/src/type/conversion.rs
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-10 19:16:51 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-10 19:16:51 +0300
commitd6d1ec80ffcc0b13234b170a91b920371078a027 (patch)
treea8347b04ad354e3f2cb265b5d506a4891853cf7f /src/type/conversion.rs
parent06798d622327707ca3f3b42d65fc3d1a25ae3c57 (diff)
downloadmyslip-d6d1ec80ffcc0b13234b170a91b920371078a027.tar.gz
myslip-d6d1ec80ffcc0b13234b170a91b920371078a027.zip
Added tests for functions
Diffstat (limited to 'src/type/conversion.rs')
-rw-r--r--src/type/conversion.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/type/conversion.rs b/src/type/conversion.rs
index 3be2874..0a96790 100644
--- a/src/type/conversion.rs
+++ b/src/type/conversion.rs
@@ -36,6 +36,8 @@ impl Type {
(a, b) if a == b => a.clone(),
+ (List(v), b) if v.len() == 1 && &v[0] == b => b.clone(),
+
(VecOf(a), VecOf(b)) => vecof(a.least_general_supertype(b)),
(List(v1), List(v2)) if v1.len() == v2.len() => {
@@ -73,6 +75,7 @@ impl Type {
/// Tries to convert this type into a subtype of the other.
///
+ /// Currently the most important conversion is from list to vec.
/// ```rust
/// use myslip::r#type::{Type::*, util::*};
///
@@ -92,6 +95,13 @@ impl Type {
/// Ok(vecof(vt("T")))
/// );
/// ```
+ ///
+ /// Though the conversion from (a) to a is also convenient:
+ /// ```rust
+ /// use myslip::r#type::{Type::*, util::*};
+ ///
+ /// assert_eq!(List(vec![Integer]).into_type(&Integer), Ok(Integer));
+ /// ```
pub fn into_type(self, other: &Type) -> Result<Type, ()> {
if !self.aka(other) {
return Err(());
@@ -103,6 +113,8 @@ impl Type {
(_, VarType(_)) => Ok(self),
+ (List(x), b) if x.len() == 1 && &x[0] == b => Ok(x[0].clone()),
+
(List(v), VecOf(b)) => match v.get(0) {
Some(first) => {
let cand = v.into_iter()