mirror of
https://github.com/erg-lang/erg.git
synced 2025-12-23 05:36:48 +00:00
chore: add record examples
This commit is contained in:
parent
5e71bd7fd0
commit
a0ce142f7b
4 changed files with 25 additions and 5 deletions
|
|
@ -1788,6 +1788,13 @@ impl Context {
|
|||
Ok(array_t(union, len))
|
||||
}
|
||||
ValueObj::Set(set) => Ok(v_enum(set)),
|
||||
ValueObj::Dict(dic) => {
|
||||
let dic = dic
|
||||
.into_iter()
|
||||
.map(|(k, v)| (TyParam::Value(k), TyParam::Value(v)))
|
||||
.collect();
|
||||
Ok(dict_t(TyParam::Dict(dic)))
|
||||
}
|
||||
ValueObj::Subr(subr) => subr.as_type(self).ok_or(ValueObj::Subr(subr)),
|
||||
other => Err(other),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1809,18 +1809,18 @@ impl Context {
|
|||
);
|
||||
record.register_trait(mono(RECORD), record_eq);
|
||||
let Slf = mono_q("Self", subtypeof(mono(RECORD)));
|
||||
let into_dict_t =
|
||||
let as_dict_t =
|
||||
fn0_met(Slf.clone(), proj_call(ty_tp(Slf), FUNC_AS_DICT, vec![])).quantify();
|
||||
let into_dict = ValueObj::Subr(ConstSubr::Builtin(BuiltinConstSubr::new(
|
||||
FUNC_UNION,
|
||||
let as_dict = ValueObj::Subr(ConstSubr::Builtin(BuiltinConstSubr::new(
|
||||
FUNC_AS_DICT,
|
||||
as_dict,
|
||||
into_dict_t,
|
||||
as_dict_t,
|
||||
None,
|
||||
)));
|
||||
record.register_py_builtin_const(
|
||||
FUNC_AS_DICT,
|
||||
Visibility::BUILTIN_PUBLIC,
|
||||
into_dict,
|
||||
as_dict,
|
||||
Some("_asdict"),
|
||||
);
|
||||
/* GenericNamedTuple */
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ use std::mem;
|
|||
use erg_common::dict::Dict;
|
||||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::{dict, Str};
|
||||
use erg_common::{enum_unwrap, set};
|
||||
|
||||
use crate::context::Context;
|
||||
|
|
@ -501,12 +502,17 @@ pub(crate) fn named_tuple_union(mut args: ValueArgs, ctx: &Context) -> EvalValue
|
|||
}
|
||||
|
||||
/// `{ .x = Int; .y = Str }.as_dict() == { "x": Int, "y": Str }`
|
||||
/// `Record.as_dict() == { Obj: Obj }`
|
||||
pub(crate) fn as_dict(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<TyParam> {
|
||||
let slf = args
|
||||
.remove_left_or_key("Self")
|
||||
.ok_or_else(|| not_passed("Self"))?;
|
||||
let fields = match ctx.convert_value_into_type(slf) {
|
||||
Ok(Type::Record(fields)) => fields,
|
||||
Ok(Type::Mono(Str::Static("Record"))) => {
|
||||
let dict = dict! { Type::Obj => Type::Obj };
|
||||
return Ok(ValueObj::builtin_type(Type::from(dict)).into());
|
||||
}
|
||||
Ok(other) => {
|
||||
return Err(type_mismatch("Record", other, "Self"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,3 +26,10 @@ assert Person!.name == Str
|
|||
for! {.x = 1; .y = 2}.as_dict().items(), ((k, v),) =>
|
||||
# k: Str, v: Int
|
||||
print! k, v
|
||||
|
||||
# {=} means the empty record (type), which is the subtype of all records
|
||||
iterate_rec! r: {=} =
|
||||
for! r.as_dict().items(), ((k, v),) =>
|
||||
print! k, v
|
||||
iterate_rec! {a = 1; b = 2}
|
||||
iterate_rec! {a = 1; b = 1.2; c = "a"}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue