M fugue.janet => fugue.janet +10 -3
@@ 884,6 884,12 @@
(errorf "Expected a %s, got: %q" ,(string proto) (,get-type-or-proto ,obj))
,(tuple obj field)))))
+(defn- maybe-get-proto-name
+ [proto-symbol]
+ # TODO: Handle the case where it's not defined.
+ (let [proto (eval proto-symbol)]
+ (proto :_name)))
+
(defmacro match
```
Prototype-aware version of `match`. Introduces one new case form:
@@ 894,12 900,13 @@
```
[x & cases]
(defn transform
+ "preprocessor for match patterns. Translates fugue patterns into std ones"
[[pattern exp]]
[(match pattern
- @[(@ '@) proto-name attrs]
+ @[(@ '@) proto-symbol attrs]
(do
- (validate-proto-match proto-name attrs)
- (struct ;(kvs attrs) :_name (string proto-name)))
+ (validate-proto-match proto-symbol attrs)
+ (struct ;(kvs attrs) :_name (maybe-get-proto-name proto-symbol)))
pattern)
exp])
M test/namespaces.janet => test/namespaces.janet +5 -0
@@ 28,4 28,9 @@
(is (= :ok (a/h :ok)))
(is (= "myname" (a/h (b/new-B :name "myname")))))
+(deftest match-with-prefix
+ (let [an-a (:new a/A)]
+ (is (= true (fugue/match an-a
+ (@ a/A {}) true)))))
+
(run-tests!)