tomhrr / dale

Lisp-flavoured C
BSD 3-Clause "New" or "Revised" License
1.03k stars 48 forks source link

Add function-local non-anonymous functions and macros #114

Closed porky11 closed 7 years ago

porky11 commented 7 years ago

so I could write a macro, that uses this function. Example for some debug functions:

(import macros)

(using-namespace std.macros
  (def print-status (macro intern (x)
    (qq printf (if (get-status (uq x)) "Correct\n" "Incorrect\n")))))

(def main (fn extern-c void (void)
  (def get-status (fn intern bool ((error-code int))
    (or (= 0 error-code) (= 47 error-code))) ;error-code 0 means no errors, 47 also can be ignored
  (def get-status (fn intern bool ((object (p void)))
    (!= object (nullptr (p void))))) ;nullptr means no object allocated, so an error
  (let ((error-code \ (f …))
        (object \ (g …)))
    (print-status error-code)
    (print-status object))))

These functions may be defined differently for different contexts, but overloading may be neccessary, because different types of objects have to be checked, so fn-pointers won't work

(some useful usecase for this may be when using the expansion of or I suggested in another issue)

tomhrr commented 7 years ago

Would it not be possible to wrap the procedure definition in a namespace, and have the relevant forms (in this case get-status) be defined within that namespace, but outside of the procedure?

porky11 commented 7 years ago

yes, seems good