9.0
top
← prev up next →

my-objectπŸ”— i

source code: https://github.com/AlexKnauth/my-object

my version of objects, inspired by (part ("(lib heresy/docs/heresy.scrbl)" "Things")) from heresy
Also, the objects here do not mean racket/class objects.

1Forms and FunctionsπŸ”— i

syntax

( object [field-idexpr]...)

creates and returns an object with the field-ids mapped to the exprs. Each expr can refer to this and all the other field-ids.

See also object-extend .

If an expr is a function, then that function can be used as a method for the object, but methods are not treated specially; they are just normal fields that happen to have functions stored in them.

Each field-id is public and immutable. Private mutable fields are also possible (see Fish example based on racket guide for objects).

syntax

( object-extend objoption...[field-idexpr]...)

option = #:inherit(inherit-id...)
| #:super([super-id1super-id2]...)
similar to both struct-copy and object inheritance.

If the #:inherit option is provided, the inherit-ids are available as bindings within the exprs.

If the #:super option is provided, the super-id1s are available within the exprs, and a super-id1 refers to the super-id2 field of the super object.

procedure

( object? v)boolean?

v:any/c
returns #t if v is an object, #f otherwise.

procedure

( object-fields obj)(hash/c symbol? any/c #:immutable#t)

obj:object?
returns a hash-table containing the fields of obj.

syntax

( send obj-exprmethod-idarg...)

(send obj-expr(identity method-expr)arg...)
(send obj-expr. field-id)
The first form is equivalent to ((obj-expr#'method-id)arg... ), or to (dynamic-send obj-expr#'method-idarg... ).

The second form allows the method to be determined at run time, and is equivalent to (dynamic-send obj-exprmethod-exprarg... ).

The third form is equivalent to (obj-expr#'field-id), and is mainly provided so that send+ can use field-id as a msg.

When send is used as an identifier by itself, it expands to dynamic-send .

syntax

( send* obj-exprmsg...)

msg = (method-idarg...)
| field-id
Equivalent to (let ([objobj-expr])(send obj. msg)... ).

syntax

( send+ obj-exprmsg...)

msg = (method-idarg...)
| field-id
Equivalent to (let* ([objobj-expr][obj(send* objmsg)]... )obj).

procedure

( dynamic-send objmethodarg...)any

obj:object?
arg:any/c
equivalent to ((objmethod)arg... ).

syntax

this

within an object or object-extend form, refers to the current object.

2ExamplesπŸ”— i

2.1Posn example based on racket guide for structsπŸ”— i

This is based on the examples from Programmer-Defined Datatypes.

Examples:
> (require my-object)
> (define p(object [x1][y2]))
> p

(object [x 1] [y 2])

> (object? p)

#t

> (p'x);by the way, you can use #'x here instead of 'x

1

> (p'y)

2

> (define p2(p'x#:->3))
> p2

(object [x 3] [y 2])

> (define (posnx0y0)
(object [xx0][yy0]
[add(λ (p)(posn(+ x(p'x))(+ y(p'y))))]
[->list(λ ()(list xy))]))
> (define p3(send (posn12)add(posn34)))
> (send p3->list)

'(4 6)

> (define p3(posn12))
> (define p4(object-extend p3[x3]));or (p3 'x #:-> 3)
> (send p4->list)

'(3 2)

> (define (3d-posnx0y0z0)
(object-extend (posnx0y0)
#:inherit(xy)
[zz0]
[add(λ (p)(3d-posn(+ x(p'x))(+ y(p'y))(+ z(p'z))))]
[->list(λ ()(list xyz))]))
> (3d-posn123)

(object [x 1] [y 2] [add #<procedure>] [->list #<procedure>] [z 3])

2.2Fish example based on racket guide for objectsπŸ”— i

This is based on the examples from Classes and Objects.

Examples:
> (require my-object)
> (define (make-fishsz)
(define sizesz);private mutable field
(object [get-size(λ ()size)]
[grow(λ (amt)
(set! size(+ amtsize)))]
[eat(λ (other-fish)
(grow(send other-fishget-size)))]))
> (define charlie(make-fish10))
> (send charlieget-size)

10

> (send charliegrow6)
> (send charlieget-size)

16

> (define (make-hungry-fishsz)
(object-extend (make-fishsz)
#:inherit(eat)
[eat-more(λ (fish1fish2)
(eatfish1)
(eatfish2))]))
> (send (make-hungry-fish32)get-size)

32

> (define (make-picky-fishsz)
(object-extend (make-fishsz)
#:super([super-growgrow])
[grow(λ (amt)
(super-grow(* 3/4amt)))]))
> (define daisy(make-picky-fish20))
> (send daisyeatcharlie)
> (send daisyget-size)

32

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /