maxim_jaffe
2020-9-13 13:23:52

Hello, can you only use make-object to get rest arguments if a class has a init-rest?


soegaard2
2020-9-13 13:26:02

maxim_jaffe
2020-9-13 13:40:57

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…


maxim_jaffe
2020-9-13 13:44:28

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…


soegaard2
2020-9-13 13:44:36

I can’t remember seeing instantiate being used - so that’s probably why it isn’t mentioned in the Guide.


maxim_jaffe
2020-9-13 13:45:24

In Python for example you have the exact same interface for function definition and method definition when it comes to args and*kwargs


maxim_jaffe
2020-9-13 13:46:32

(and __init__ method)


maxim_jaffe
2020-9-13 13:47:17

I don’t quite get how the same functionality in racket is spread over init init-field init-rest and to some degree field


soegaard2
2020-9-13 13:49:20

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.


soegaard2
2020-9-13 13:50:25

However, I have had success finding examples for seldomly used constructs by searching on Github with language:racket as one of the search terms.


maxim_jaffe
2020-9-13 13:51:02

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


maxim_jaffe
2020-9-13 13:51:25

I actually tried that for init-rest but I mostly got code in which it’s used for syntax


maxim_jaffe
2020-9-13 13:52:36

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


soegaard2
2020-9-13 13:53:24

If no-one turns up with the answer, try the mailing list.


maxim_jaffe
2020-9-13 13:54:50

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..!


dedbox
2020-9-13 17:20:34

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.


dedbox
2020-9-13 17:40:20

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 instantiateinstantiate is a much heftier wrapper around make-objectmake-object is a fancy macro that expands to a function that does all the work, presumably to do some compile-time checking.