6.3.90.900
dotmethod
This package provides both a dotmethod/dotmethod module to be required and a #lang dotmethod meta-language.
Both of these provide functionality for obj.method(x)-style method definitions and method calls, but generalized to be used for all data types that can be distinguished by predicates.
1 dotmethod/dotmethod
(require dotmethod/dotmethod) | package: dotmethod |
A require module intended to be used along with the
sweet-exp and postfix-dot-notation
meta-languages together, for example:
#lang postfix-dot-notation sweet-exp racket require dotmethod/dotmethod
syntax
#lang postfix-dot-notation sweet-exp racket require dotmethod/dotmethod dotmethod pred-expr obj-id.method(args ...) method-body ...+ ;or dotmethod pred-expr obj-id.method method-expr ; a function expression
Examples:
>
require dotmethod/dotmethod
>
dotmethod list? lst.ref(i) (list-ref lst i)
>
dotmethod hash? hsh.ref (case-lambda ; can use case-lambda or any other expression here [(key) (hash-ref hsh key)] [(key else) (hash-ref hsh key else)])
>
let ([lox '(a b c d)]) lox.ref(2) ; you could also write (lox.ref 2) or lox.ref 2 if you prefer ; this actually means ((ref lox) 2) in normal lisp notation 'c
>
let ([ht (hash 'a 1 'b 2 'c 3)]) values ht.ref('a) ht.ref('d 'not-found) 1
'not-found
>
define adder% class object% (super-new) init-field a define/public add(b) (+ a b)
>
dotmethod (is-a?/c adder%) adder.add(b) (send adder add b)
>
define five-adder (make-object adder% 5)
>
five-adder.add(2) 7
syntax
#lang postfix-dot-notation sweet-exp racket require dotmethod/dotmethod dotmethods pred-expr obj-id.method(args ...) method-body ...+ ...
Examples:
>
require dotmethod/dotmethod
>
dotmethods list? lst.ref(i) (list-ref lst i) hash? hsh.ref (case-lambda [(key) (hash-ref hsh key)] [(key else) (hash-ref hsh key else)])
>
let ([lox '(a b c d)]) lox.ref(2) 'c
>
let ([ht (hash 'a 1 'b 2 'c 3)]) ht.ref('a) 1
2 #lang dotmethod
A meta-language for dotmethods so that
Is roughly equivalent to
I say roughly, because unlike with just blindly adding a require, it still works properly if your-base-language doesn’t allow require forms to be inserted like that.
Example: