Skip to content

Commit

Permalink
add type scope
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Jan 11, 2025
1 parent 0c3ffa2 commit f0b9f21
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/types/custom.typ
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
typecheck: true,
allow-unknown-fields: false,
construct: none,
scope: none,
casts: none,
fold: none,
) = {
Expand All @@ -49,6 +50,7 @@
assert(type(allow-unknown-fields) == bool, message: "types.declare: the 'allow-unknown-fields' argument must be a boolean.")
assert(construct == none or type(construct) == function, message: "types.declare: 'construct' must be 'none' (use default constructor) or a function receiving the original constructor and returning the new constructor.")
assert(default == none or type(default) == function, message: "types.declare: 'default' must be none or a function receiving the constructor and returning the default.")
assert(scope == none or type(scope) in (dictionary, module), message: "types.declare: 'scope' must be either 'none', a dictionary or a module")
assert(
casts == none
or type(casts) == array and casts.all(
Expand Down Expand Up @@ -114,6 +116,7 @@
id: typeid,
// We will add this here once the constructor is declared
typeinfo: none,
scope: scope,
parse-args: parse-args,
default-fields: default-fields,
all-fields: all-fields,
Expand Down Expand Up @@ -256,6 +259,7 @@
default-constructor: default-constructor,
tid: tid,
id: (tid: tid, name: name),
scope: scope,
fields-known: true,
valid: true
)
Expand Down
Binary file added test/unit/types/custom/scope/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions test/unit/types/custom/scope/scope.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#let value = 50

#let do-math(a, b) = a + b

#import "/src/lib.typ" as e: field

#let subperson = e.types.declare(
"subperson",
fields: (
field("name", str, required: true),
field("age", int, required: true),
field("border", stroke, default: 5pt),
),
prefix: "mypkg"
)

#let subwock = e.element.declare(
"subwock",
display: it => {
assert.eq(it.color, blue)
assert.eq(it.inner, [Hello!])
},
fields: (
field("color", color, default: red),
field("inner", content, default: [Hello!])
),
prefix: ""
)
31 changes: 31 additions & 0 deletions test/unit/types/custom/scope/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: types, field
#import types: native
#import "/src/types/types.typ": cast

#let person = types.declare(
"person",
fields: (
field("name", str, required: true),
field("age", int, required: true),
field("border", stroke, default: 5pt),
),
scope: {
import "scope.typ"
scope
},
prefix: "mypkg"
)

#let person-scope = e.scope(person)

#assert.eq(e.scope(person("John", 100)), person-scope)

#person-scope.subwock(color: blue)

#assert.eq(e.fields(person-scope.subperson("a", 5)), (name: "a", age: 5, border: stroke(5pt)))

#assert.eq(person-scope.do-math(2, 3), 5)
#assert.eq(person-scope.value, 50)

0 comments on commit f0b9f21

Please sign in to comment.