~subsetpark/fugue

2f3909210b2002f2d4f389d5a40017d1ba943f89 — Zach Smith 5 months ago d0d4dc0
Tweak with-slots: @ by itself should be this
3 files changed, 31 insertions(+), 27 deletions(-)

M README.md
M fugue.janet
M test/fugue.janet
M README.md => README.md +13 -11
@@ 84,7 84,7 @@ runtime, checks that `some-object` is a descendent of
`SomePrototype` and if so, translates to `(some-object
:some-field)`.

[1]: fugue.janet#L718
[1]: fugue.janet#L721

## Root



@@ 430,14 430,16 @@ Anaphoric macro with transformed getter/setters.

Introduces two useful forms for referring to `obj`.

The reference symbol, `@` by default (see `with-slots-as` to specify
the symbol), in a 1-tuple: `(@)` will be translated into a
reference to `obj`.
It introduces a *reference symbol* - `@` by default
(see `with-slots-as `to specify the symbol).

The pattern `(@ <field name>)` is transformed into `(this (keyword
<field name>))`, if and only if `<field name>` is defined for
`proto`, so that `(@ name)` or its setter form `(set (@ name) foo)`
do the right thing.
The pattern `(@ <field name>)`, where `<field name>` is a symbol, is
transformed into `(obj (keyword <field name>))`, if and only if
`<field name>` is defined for `proto`, so that `(@ name)` or its
setter form `(set (@ name) foo)` do the right thing.

The reference symbol by itself is introduces as a reference to
`obj`.

Returns `obj`.



@@ 450,13 452,13 @@ repl:2:> (defproto Foo nil name {:default "Jane Doe"})
repl:4:> (with-slots Foo (new-Foo)
           (set (@ name) "Cosmo Kramer")
           (print (@ name))
           (print (Foo? (@))))
           (print (Foo? @)))
Cosmo Kramer
true
@Foo{:_meta @{:object-type :instance} :name "Cosmo Kramer"}
```

[17]: fugue.janet#L672
[17]: fugue.janet#L673

## with-slots-as



@@ 472,5 474,5 @@ Specifies `as` as the reference symbol for `with-slots`.

See `with-slots` documentation for more details.

[18]: fugue.janet#L707
[18]: fugue.janet#L710


M fugue.janet => fugue.janet +14 -12
@@ 645,10 645,7 @@
  [fields obj-sym as proto-name]
  (fn [sym]
    (cond
      (and (tuple? sym) (= (length sym) 1) (= (sym 0) as))
      obj-sym

      (and (tuple? sym) (= (length sym) 2) (= (sym 0) as))
      (and (tuple? sym) (= (length sym) 2) (= (sym 0) as) (symbol? (sym 1)))
      (let [field-name (-> sym (1) (keyword))]
        (unless (index-of field-name fields)
          (errorf `Encountered field reference %q for prototype %q;


@@ 659,6 656,9 @@
                  fields))
        ~(,obj-sym ,field-name))

      (= sym as)
      obj-sym

      true
      sym)))



@@ 676,14 676,16 @@

  Introduces two useful forms for referring to `obj`.

  The reference symbol, `@` by default (see `with-slots-as` to specify
  the symbol), in a 1-tuple: `(@)` will be translated into a
  reference to `obj`.
  It introduces a *reference symbol* - `@` by default
  (see `with-slots-as `to specify the symbol).

  The pattern `(@ <field name>)`, where `<field name>` is a symbol, is
  transformed into `(obj (keyword <field name>))`, if and only if
  `<field name>` is defined for `proto`, so that `(@ name)` or its
  setter form `(set (@ name) foo)` do the right thing.

  The pattern `(@ <field name>)` is transformed into `(obj (keyword
  <field name>))`, if and only if `<field name>` is defined for
  `proto`, so that `(@ name)` or its setter form `(set (@ name) foo)`
  do the right thing.
  The reference symbol by itself is introduces as a reference to
  `obj`.

  Returns `obj`.
  


@@ 696,7 698,7 @@
  repl:4:> (with-slots Foo (new-Foo)
             (set (@ name) "Cosmo Kramer")
             (print (@ name))
             (print (Foo? (@))))
             (print (Foo? @)))
  Cosmo Kramer
  true
  @Foo{:_meta @{:object-type :instance} :name "Cosmo Kramer"}

M test/fugue.janet => test/fugue.janet +4 -4
@@ 354,8 354,8 @@
    (def res (fugue/with-slots SlotHaver a-slot-haver
                               (set (@ name) "will shortz")
                               (is (= "will shortz" (@ name)))
                               (is (= "will shortz" ((@) :name)))
                               (is (= "will shortz" (name (@))))))
                               (is (= "will shortz" (@ :name)))
                               (is (= "will shortz" (name @)))))
    (is (= res a-slot-haver))
    (is (= "will shortz" (a-slot-haver :name)))))



@@ 364,8 364,8 @@
    (def res (fugue/with-slots-as SlotHaver a-slot-haver s
                               (set (s name) "will shortz")
                               (is (= "will shortz" (s name)))
                               (is (= "will shortz" ((s) :name)))
                               (is (= "will shortz" (name (s))))))
                               (is (= "will shortz" (s :name)))
                               (is (= "will shortz" (name s)))))
    (is (= res a-slot-haver))
    (is (= "will shortz" (a-slot-haver :name)))))