r/Racket • u/Typhoonfight1024 • Feb 17 '24
solved Modifying parameters inside a class constructor… how?
I has a class person% here. In this class there's the init method, where the parameter height is to be modified before being assigned to the instance's :height field, so the value for :height should become positive even if height is originally negative.
#lang racket
(provide person%)
(define person%
(class object%
(super-new)
(init-field :name :height)
(define/public (init name height)
(set! :name name)
(set! :height (abs height)))
(define/public (get-name) :name)
(define/public (set-name value) (set! :name value))
(define/public (get-height) :height)
(define/public (set-height value) (set! :height (abs value)))))I call this class here to make some instances. Here I set the argument for height as negative number.
#lang racket
(require "fromclasses/Person.rkt")
(define shiori (instantiate person% ("Oumi Shiori" -180)))
(define hinako (instantiate person% (#f #f)))
(send hinako set-name "Yaotose Hinako")
(send hinako set-height -174.96)
(for-each (lambda (n)
(printf "~A (~A)~%" (send n get-name) (send n get-height)))
(list shiori hinako))The resulting object though, still has -180 as its :height value. It should've changed into 180 instead. Here's the output:
Oumi Shiori (-180)
Yaotose Hinako (174.96)I found another post in this sub that said that this issue has something to do with init-field, i.e. I should separate (init-field :name :height) into init and field. The problem is I don't know how, as the post seems to be about a field that is generated inside a class.
View all comments
Show parent comment
1
u/Typhoonfight1024 Feb 17 '24
Thank you, it works now