Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

TXR Lisp supports a form of RAII:

  This is the TXR Lisp interactive listener of TXR 181.
  Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet.
  1> (defstruct raii-class nil
       (:init (me) (put-line "raii-class hello"))
       (:fini (me) (put-line "raii-class goodbye")))
  #<struct-type raii-class>
  2> (with-objects ((o (new raii-class)))
       (put-line "inside block"))
  raii-class hello
  inside block
  raii-class goodbye
  t
Constructor abort via non-local transfer:

  3> (defstruct ctor-throws nil
       (:init (me) (error "refuse to init"))
       (:fini (me) (put-line "ctor-throws goodbye")))
  #<struct-type ctor-throws>
  4> (new ctor-throws)
  ctor-throws goodbye
  ** refuse to init
  ** during evaluation at expr-3:2 of form (error "refuse to init")
:fini called by GC:

  5> (progn (new raii-class) nil)
  raii-class hello
  nil
  6> (sys:gc)
  raii-class goodbye
  t
with-objects is not just with structs but for anything with a GC finalizer, like the (1 2 3) list in this example:

  6> (with-objects ((o (new raii-class))
                    (p [finalize (list 1 2 3) prinl]))
       (put-line "inside block"))
  raii-class hello
  inside block
  (1 2 3)
  raii-class goodbye
  t
with-objects can't be used for defining function arguments, but it can refer to variables in scope; resource not defined in the block can be finalized:

  11> (let ((x (new raii-class)))
        (with-objects ((x x))
          (put-line "inside-block")))
  raii-class hello
  inside-block
  raii-class goodbye
  t
Parameter passing has reference semantics so the smart-pointer style RAII use of C++ across function interfaces is not really applicable.

We don't want to copy a struct argument when a function is called and be incrementing refcounts on things the struct's slots point to; that's just stupid.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: