Skip to content

Commit

Permalink
feat: filter method can also narrow types
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Apr 4, 2024
1 parent a7fb03c commit 7f29316
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 4 additions & 4 deletions crates/erg_compiler/context/initialize/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use erg_common::log;
use crate::ty::constructors::*;
use crate::ty::typaram::TyParam;
use crate::ty::value::ValueObj;
use crate::ty::{CastTarget, Field, GuardType, Type, Visibility};
use crate::ty::{CastTarget, Field, Type, Visibility};
use Type::*;

use crate::context::initialize::*;
Expand Down Expand Up @@ -108,14 +108,14 @@ impl Context {
poly(ENUMERATE, vec![ty_tp(T.clone())]),
)
.quantify();
let guard = Type::Guard(GuardType::new(
let grd = guard(
"<builtins>".into(),
CastTarget::arg(0, "x".into(), Location::Unknown),
U.clone(),
));
);
let t_filter = nd_func(
vec![
kw(KW_FUNC, nd_func(vec![kw("x", T.clone())], None, guard)),
kw(KW_FUNC, nd_func(vec![kw("x", T.clone())], None, grd)),
kw(KW_ITERABLE, poly(ITERABLE, vec![ty_tp(T.clone())])),
],
None,
Expand Down
19 changes: 15 additions & 4 deletions crates/erg_compiler/context/initialize/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use erg_common::log;

use crate::ty::constructors::*;
use crate::ty::value::ValueObj;
use crate::ty::{Type, Visibility};
use crate::ty::{CastTarget, Type, Visibility};
use ParamSpec as PS;
use Type::*;

Expand Down Expand Up @@ -249,12 +249,23 @@ impl Context {
Visibility::BUILTIN_PUBLIC,
Some("Function::iterable_map"),
);
let grd = guard(
"<builtins>".into(),
CastTarget::arg(0, "x".into(), Location::Unknown),
U.clone(),
);
let t_filter = fn1_met(
Slf.clone(),
func1(T.clone(), Bool),
poly(FILTER, vec![ty_tp(T.clone())]),
nd_func(vec![kw("x", T.clone())], None, grd),
poly(FILTER, vec![ty_tp(T.clone() & U.clone())]),
)
.quantify();
.quantify()
& fn1_met(
Slf.clone(),
func1(T.clone(), Bool),
poly(FILTER, vec![ty_tp(T.clone())]),
)
.quantify();
iterable.register_builtin_decl(
FUNC_FILTER,
t_filter,
Expand Down

0 comments on commit 7f29316

Please sign in to comment.