Hello, can you only use make-object
to get rest arguments if a class has a init-rest?
Well the problem is in the guide instantiate
or make-object
are not at all introduced… And in the reference besides the blue boxes there doesn’t seem to be a single example of instantiate
, and the only example I found for make-object
is exactly for init-rest
… I like the guide, but it’s a bit sparse when it comes to classes, and the reference I find at times hard to understand… I just found it confusing that new
doesn’t take rest arguments…
First time I tried Racket about a year back I gave up precisely because of classes… Coming from Python I just found initialization confusing (only recently I understood I could use init-field (again no examples in the guide). The essay https://felleisen.org/matthias/Class/programming-with-class/ helped quite a bit, but no example of init-rest there for example…
I can’t remember seeing instantiate
being used - so that’s probably why it isn’t mentioned in the Guide.
In Python for example you have the exact same interface for function definition and method definition when it comes to args and*kwargs
(and __init__
method)
I don’t quite get how the same functionality in racket is spread over init
init-field
init-rest
and to some degree field
The reason for the missing examples is probably due to the power of structures. It is quite common to use plain structures instead of objects. This implies that there are fewer examples/tutorials on the class/object system than you see for other languages. Myself, I almost exclusively use classes when working with the gui.
However, I have had success finding examples for seldomly used constructs by searching on Github with language:racket
as one of the search terms.
Well I wanted to use struct
but I wanted to get more flexibility when it came to optional arguments I end up opting for class
I actually tried that for init-rest
but I mostly got code in which it’s used for syntax
In fact the first example I got was the definition files of classes in typed racket :sweat_smile: https://github.com/search?p=1&q=language%3Aracket+class+init-rest&type=Code
If no-one turns up with the answer, try the mailing list.
well for now I got that I have to use make-object
to use rest arguments, I just found it a bit strange I couldn’t use new
..!
Hi @maxim_jaffe, I found this at the top of Reference, section 6.3 (https://docs.racket-lang.org/reference/objcreation.html):
> The https://docs.racket-lang.org/reference/objcreation.html#%28def._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._make-object%29%29\|make-object procedure creates a new object with by-position initialization arguments, the https://docs.racket-lang.org/reference/objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._new%29%29\|new form creates a new object with by-name initialization arguments, and the https://docs.racket-lang.org/reference/objcreation.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._instantiate%29%29\|instantiate form creates a new object with both by-position and by-name initialization arguments. The reference entry for new
says it’s syntax — it’s a macro, not a function — and rest args are for functions. I’m guessing new
and instantiate
are macros that expand to uses of make-object
, but I’d check the Racket source if I needed to be sure.
In case you’re interested, I took a look at the source and here’s what I found (https://github.com/racket/racket/blob/aafdafb1cf04162fbcb203e10ebb773a1e78c63b/racket/collects/racket/private/class-internal.rkt#L3336):
• new
is a thin wrapper around instantiate
• instantiate
is a much heftier wrapper around make-object
• make-object
is a fancy macro that expands to a function that does all the work, presumably to do some compile-time checking.