Skip to content

Closures in inception need improvement #191

Description

@skx

At the moment there is support for lambdas and closures in inception so this works:

(defun makeAdder (n)
  (lambda (x)
   (+ x n)))

(defun main ()
  (let ((ten (makeAdder 10)))
  (println (ten 15))
  (println (ten 25))
  ))

However the captured values are essentially read-only, so this example fails:

 (defun counter ()
   (let ((n 0))
     (lambda ()
       (set! n (+ n 1))
      n)))

 (defun main()
   (let ((f (counter)))
     (println (f))
     (println (f))))

I think this should be an easy fix, since we're passing around env everywhere. While we cannot have a closure modify a single value in the parent scope we do have the ability to change things in-place for lists, via nth! and our closure is itself a list:

(defun closure (params body env)
(list "closure" params body env))

When the closure runs it should mutate its own env, and that would be enough?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions