mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
add inspect implementation for dict and set
This commit is contained in:
parent
79a58843b5
commit
9b181e1b3f
5 changed files with 1239 additions and 2 deletions
|
@ -32,6 +32,7 @@ interface Dict
|
|||
Str,
|
||||
Num.{ Nat, U64, U8, I8 },
|
||||
Hash.{ Hasher, Hash },
|
||||
Inspect.{ Inspect, Inspector, InspectFormatter },
|
||||
]
|
||||
|
||||
## A [dictionary](https://en.wikipedia.org/wiki/Associative_array) that lets you
|
||||
|
@ -108,6 +109,9 @@ Dict k v := {
|
|||
Hash {
|
||||
hash: hashDict,
|
||||
},
|
||||
Inspect {
|
||||
toInspector: toInspectorDict,
|
||||
},
|
||||
]
|
||||
|
||||
isEq : Dict k v, Dict k v -> Bool where k implements Hash & Eq, v implements Eq
|
||||
|
@ -126,6 +130,12 @@ isEq = \xs, ys ->
|
|||
hashDict : hasher, Dict k v -> hasher where k implements Hash & Eq, v implements Hash, hasher implements Hasher
|
||||
hashDict = \hasher, dict -> Hash.hashUnordered hasher (toList dict) List.walk
|
||||
|
||||
|
||||
toInspectorDict : Dict k v -> Inspector f where k implements Inspect & Hash & Eq, v implements Inspect, f implements InspectFormatter
|
||||
toInspectorDict = \dict ->
|
||||
fmt <- Inspect.custom
|
||||
Inspect.apply (Inspect.dict dict walk Inspect.toInspector Inspect.toInspector) fmt
|
||||
|
||||
## Return an empty dictionary.
|
||||
## ```
|
||||
## emptyDict = Dict.empty {}
|
||||
|
|
|
@ -25,6 +25,7 @@ interface Set
|
|||
Dict.{ Dict },
|
||||
Num.{ Nat },
|
||||
Hash.{ Hash, Hasher },
|
||||
Inspect.{ Inspect, Inspector, InspectFormatter },
|
||||
]
|
||||
|
||||
## Provides a [set](https://en.wikipedia.org/wiki/Set_(abstract_data_type))
|
||||
|
@ -37,6 +38,9 @@ Set k := Dict.Dict k {} where k implements Hash & Eq
|
|||
Hash {
|
||||
hash: hashSet,
|
||||
},
|
||||
Inspect {
|
||||
toInspector: toInspectorSet,
|
||||
},
|
||||
]
|
||||
|
||||
isEq : Set k, Set k -> Bool where k implements Hash & Eq
|
||||
|
@ -53,6 +57,11 @@ isEq = \xs, ys ->
|
|||
hashSet : hasher, Set k -> hasher where k implements Hash & Eq, hasher implements Hasher
|
||||
hashSet = \hasher, @Set inner -> Hash.hash hasher inner
|
||||
|
||||
toInspectorSet : Set k -> Inspector f where k implements Inspect & Hash & Eq, f implements InspectFormatter
|
||||
toInspectorSet = \set ->
|
||||
fmt <- Inspect.custom
|
||||
Inspect.apply (Inspect.set set walk Inspect.toInspector) fmt
|
||||
|
||||
## Creates a new empty `Set`.
|
||||
## ```
|
||||
## emptySet = Set.empty {}
|
||||
|
|
|
@ -29,8 +29,8 @@ pub(crate) fn derive_to_inspector(
|
|||
) -> DerivedBody {
|
||||
let (body, body_type) = match key {
|
||||
FlatInspectableKey::List() => to_inspector_list(env, def_symbol),
|
||||
FlatInspectableKey::Set() => todo!(),
|
||||
FlatInspectableKey::Dict() => todo!(),
|
||||
FlatInspectableKey::Set() => unreachable!(),
|
||||
FlatInspectableKey::Dict() => unreachable!(),
|
||||
FlatInspectableKey::Record(fields) => {
|
||||
// Generalized record var so we can reuse this impl between many records:
|
||||
// if fields = { a, b }, this is { a: t1, b: t2 } for fresh t1, t2.
|
||||
|
|
1202
crates/compiler/test_mono/generated/inspect_derived_dict.txt
Normal file
1202
crates/compiler/test_mono/generated/inspect_derived_dict.txt
Normal file
File diff suppressed because it is too large
Load diff
|
@ -3420,3 +3420,19 @@ fn inspect_derived_list() {
|
|||
"#
|
||||
)
|
||||
}
|
||||
|
||||
#[mono_test(large_stack = "true")]
|
||||
fn inspect_derived_dict() {
|
||||
indoc!(
|
||||
r#"
|
||||
app "test"
|
||||
imports []
|
||||
provides [main] to "./platform"
|
||||
|
||||
main =
|
||||
Dict.fromList [("a", 1), ("b", 2)]
|
||||
|> Inspect.inspect
|
||||
|> Inspect.toDbgStr
|
||||
"#
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue