0

The following macro tries to assign a member variable from init argument.

But

name 'self' is not defined

(defmacro optional_assign [x &optional [base self]]
    `(lif ~x (setv (. ~base ~x) ~x) (setv (. ~base ~x ) None) ))

(defclass clsa []
    (defn __init__ [self &optional y]
      (optional_assign y) 
      ))

(setv insa1 (clsa 123))
(print insa1.y) ;;=>123  
(setv insa2 (clsa))
(print insa2.y) ;;=>None
niitsuma
  • 117
  • 1
  • 4

1 Answers1

1

The default argument is evaluated like an ordinary expression, so you want [base 'self], not [base self].

Also, you're missing a ~ for the first mention of x in the body.

Kodiologist
  • 2,984
  • 18
  • 33