-> operator better explained

This commit is contained in:
2025-07-13 22:11:10 +02:00
parent de1d245af3
commit 6ce0a99f15

View File

@@ -33,8 +33,9 @@ Otherwise, it falls back to the original @racket[send] from @racket[racket/class
] ]
@defidform[->]{(-> obj method arg ...) @defidform[->]{(-> obj method arg ...)
Similar to @racket[send], but uses a cleaner Racket-style method call syntax. An alternative method dispatch macro. The syntax omits the explicit @racket[send] and may feel more
Dispatches to either Roos or Racket based on the object type.} familiar to users of other programming languages that use concise or operator-based method invocation.
This macro checks whether @racket[obj] is a Roos object or a standard Racket object and dispatches accordingly.}
@examples[ @examples[
#:eval (make-base-eval '(require roos/class)) #:eval (make-base-eval '(require roos/class))
@@ -45,6 +46,18 @@ Dispatches to either Roos or Racket based on the object type.}
(-> o f 3) ; → 15 (-> o f 3) ; → 15
] ]
@subsection{Comparison with other languages}
In other languages, method invocation often uses compact notation:
@itemlist[
@item{Perl: @tt{$obj->method(args)}}
@item{Python / Ruby / Java / JavaScript: @tt{obj.method(args)}}
@item{C++: @tt{obj->method(args)} or @tt{obj.method(args)}}
]
The @racket[->] macro serves a similar role within the s-expression syntax of Racket.
@defidform[new]{(new class arg ...) @defidform[new]{(new class arg ...)
Creates a new object. If @racket[class] is a Roos class (@racket[roos-class?]), then @racket[roos-new] is used. Creates a new object. If @racket[class] is a Roos class (@racket[roos-class?]), then @racket[roos-new] is used.
Otherwise, the standard @racket[new] from @racket[racket/class] is used, supporting initialization arguments such as @racket[(init-field val)].} Otherwise, the standard @racket[new] from @racket[racket/class] is used, supporting initialization arguments such as @racket[(init-field val)].}