Skip to content

Commit

Permalink
!204 Keep box simple and stupid in (liii lang)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Feb 19, 2025
1 parent f359547 commit 7d6cb5f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
14 changes: 4 additions & 10 deletions GoldfishLang.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -4348,11 +4348,9 @@
<paragraph|box>

<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
(define (box x . xs)
(define (box x)

\ \ (let1 r

\ \ \ \ \ \ (cond ((integer? x) (rich-integer x))
\ \ (cond ((integer? x) (rich-integer x))

\ \ \ \ \ \ \ \ ((char? x) (rich-char (char-\<gtr\>integer x)))

Expand All @@ -4364,16 +4362,14 @@

\ \ \ \ \ \ \ \ ((hash-table? x) (rich-hash-table x))

\ \ \ \ \ \ \ \ (else (type-error "box: x must be integer?, char?, string?, list?, vector?, hash-table?")))

\ \ \ \ \ \ \ (if (null? xs) r (apply r xs))))
\ \ \ \ \ \ \ \ (else (type-error "box: x must be integer?, char?, string?, list?, vector?, hash-table?"))))

\;
</goldfish-chunk>

<paragraph|$ 语法糖>

$“装饰”这个字面量,使其成为 rich 的。可以去掉box的括号,进一步简化语法
$语法糖将第一个参数装箱(boxing),得到一个样本类,并将剩下的参数作为样本类的参数。该语法糖简化了语法,同时避免了隐式转换

<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
(define ($ x . xs)
Expand All @@ -4383,8 +4379,6 @@
\;
</goldfish-chunk>

<paragraph|测试<math|> $ 语法糖>

<\scm-chunk|tests/goldfish/liii/lang-test.scm|true|true>
(check ($ 1 :to 3) =\<gtr\> '(1 2 3))

Expand Down
8 changes: 3 additions & 5 deletions goldfish/liii/lang.scm
Original file line number Diff line number Diff line change
Expand Up @@ -958,16 +958,14 @@

)

(define (box x . xs)
(let1 r
(cond ((integer? x) (rich-integer x))
(define (box x)
(cond ((integer? x) (rich-integer x))
((char? x) (rich-char (char->integer x)))
((string? x) (rich-string x))
((list? x) (rich-list x))
((vector? x) (rich-vector x))
((hash-table? x) (rich-hash-table x))
(else (type-error "box: x must be integer?, char?, string?, list?, vector?, hash-table?")))
(if (null? xs) r (apply r xs))))
(else (type-error "box: x must be integer?, char?, string?, list?, vector?, hash-table?"))))

(define ($ x . xs)
(if (null? xs) (box x) (apply (box x) xs)))
Expand Down

0 comments on commit 7d6cb5f

Please sign in to comment.