~subsetpark/fugue

7a4f5669ca327188d96299ea1d29617dd461bc65 — Zach Smith 6 months ago 670edce with-slots
Add some with-slots documentation
2 files changed, 96 insertions(+), 14 deletions(-)

M README.md
M fugue.janet
M README.md => README.md +77 -13
@@ 57,7 57,7 @@ be selected for any descendent prototype instances.

## fugue

[Root](#Root), [Root*?](#Root-1), [Root?](#Root-2), [allocate](#allocate), [declare-open-multi](#declare-open-multi), [defgeneric](#defgeneric), [defmethod](#defmethod), [defmulti](#defmulti), [defproto](#defproto), [extend-multi](#extend-multi), [fields](#fields), [get-type-or-proto](#get-type-or-proto), [multimethod-types-match?](#multimethod-types-match), [prototype?](#prototype)
[Root](#Root), [Root*?](#Root-1), [Root?](#Root-2), [allocate](#allocate), [declare-open-multi](#declare-open-multi), [defgeneric](#defgeneric), [defmethod](#defmethod), [defmulti](#defmulti), [defproto](#defproto), [extend-multi](#extend-multi), [fields](#fields), [get-type-or-proto](#get-type-or-proto), [multimethod-types-match?](#multimethod-types-match), [new-Root](#new-Root), [prototype?](#prototype), [with-slots](#with-slots), [with-slots-as](#with-slots-as)

## Root



@@ 112,7 112,7 @@ specific prototype for this key, then `fugue/allocate` will put `value`
at `key` in the appropriate prototype, and it will be inherited by
all descendents of that prototype.

[4]: fugue.janet#L285
[4]: fugue.janet#L287

## declare-open-multi



@@ 128,7 128,7 @@ Extending an open multimethod (see `extend-multi`) from any other
environment makes the case extension available wherever the
multimethod has been imported.

[5]: fugue.janet#L598
[5]: fugue.janet#L600

## defgeneric



@@ 143,7 143,7 @@ first argument has a method corresponding to the name of the
function, call that object 's method with the arguments. Otherwise,
evaluate `body`.

[6]: fugue.janet#L319
[6]: fugue.janet#L321

## defmethod



@@ 161,7 161,7 @@ Defines a few symbols for reference in the body of the method.
`__parent` - Bound to the parent of `proto`.
`__super` - Bound to the method at `name` within `__parent`.

[7]: fugue.janet#L330
[7]: fugue.janet#L332

## defmulti



@@ 233,7 233,7 @@ repl:12:> (cat "hello" 100)
"hello #100"
```

[8]: fugue.janet#L522
[8]: fugue.janet#L524

## defproto



@@ 252,7 252,7 @@ named after it.

`parent-name` is required; it can be an existing prototype, *or*
some null-ish value. If null-ish (`nil` or `()` should make the most
                                        sense...) the parent of the prototype will be set to `fugue/Root`.
sense...) the parent of the prototype will be set to `fugue/Root`.

`fields` should be 0 or more pairs of the following format:



@@ 331,7 331,7 @@ See that function's documentation for full usage reference.
Whenever a case is added to `multi`, that case is available
wherever the multimethod is imported.

[10]: fugue.janet#L614
[10]: fugue.janet#L616

## fields



@@ 344,7 344,7 @@ wherever the multimethod is imported.
Return all the defined fields for `obj` and its prototype
hierarchy.

[11]: fugue.janet#L357
[11]: fugue.janet#L359

## get-type-or-proto



@@ 357,7 357,7 @@ hierarchy.
Return the prototype of `obj`, if it has one, otherwise the keyword
output of `type`.

[12]: fugue.janet#L109
[12]: fugue.janet#L101

## multimethod-types-match?



@@ 371,17 371,81 @@ Check to see if the types `args` match the sequence `arg-types`,
according to multimethod rules (ie, following prototype membership
and using `:_` as a fallback)

[13]: fugue.janet#L450
[13]: fugue.janet#L452

## prototype?
## new-Root

**function**  | [source][14]

```janet
(new-Root & rest)
```



[14]: eval#L-1

## prototype?

**function**  | [source][15]

```janet
(prototype? obj)
```

Is `obj` the result of a `defproto ` call? 

[14]: fugue.janet#L278
[15]: fugue.janet#L279

## with-slots

**macro**  | [source][16]

```janet
(with-slots proto obj & body)
```

Anaphoric macro with transformed getter/setters.

Injects `this` into scope as a reference to `obj`.

Any symbols that begin with `@` are transformed into `(this <field name>)`,
so that `@name` or its setter form `(set @name foo)` do the right thing.

---

Example :

```
repl:2:> (defproto Foo nil name {:default "Jane Doe"})
repl:3:> (def f (new-Foo))
repl:4:> (with-slots Foo f
           (set @name "Cosmo Kramer")
           (print @name)
           (print (Foo? this)))
Cosmo Kramer
true
nil
```

[16]: fugue.janet#L658

## with-slots-as

**macro**  | [source][17]

```janet
(with-slots-as proto obj as & body)
```

Anaphoric macro with transformed getter/setters.

Injects the arg `as` into scope as a reference to `obj`.

Any symbols that begin with `@` are transformed into `(<as> <field name>)`,
so that `@name` or its setter form `(set @name foo)` do the right thing.

See `with-slots` documentation for more details.

[17]: fugue.janet#L686


M fugue.janet => fugue.janet +19 -1
@@ 656,14 656,30 @@
       ,;(prewalk f body))))

(defmacro with-slots
  ```
  ````
  Anaphoric macro with transformed getter/setters.

  Injects `this` into scope as a reference to `obj`.

  Any symbols that begin with `@` are transformed into `(this <field name>)`,
  so that `@name` or its setter form `(set @name foo)` do the right thing.

  ---

  Example :

  ```
  repl:2:> (defproto Foo nil name {:default "Jane Doe"})
  repl:3:> (def f (new-Foo))
  repl:4:> (with-slots Foo f
             (set @name "Cosmo Kramer")
             (print @name)
             (print (Foo? this)))
  Cosmo Kramer
  true
  nil
  ```
  ````
  [proto obj & body]
  (with-slots-as proto obj 'this body))



@@ 675,6 691,8 @@

  Any symbols that begin with `@` are transformed into `(<as> <field name>)`,
  so that `@name` or its setter form `(set @name foo)` do the right thing.

  See `with-slots` documentation for more details.
  ```
  [proto obj as & body]
  (with-slots-as proto obj as body))