JanetDocsPlaygroundI'm feeling luckyGitHub sign in

with



    macro
    boot.janet on line 369, column 1

    (with [binding ctor dtor] & body)

    Evaluate body with some resource, which will be automatically 
    cleaned up if there is an error in body. binding is bound to the 
    expression ctor, and dtor is a function or callable that is passed 
    the binding. If no destructor (dtor) is given, will call :close on 
    the resource.


2 examplesSign in to add an example
Loading...
# Setup
(spit "poem.txt" "I’ve decided to tackle it\nIt’s going to get done")

# A version of head -n 1, but entirely in Janet
# Note, that because files in Janet support :close as a method
# we don't have to define a dtor. 
# There might be other closing work to do, of course
(with [f (file/open "poem.txt")] (print (:read f :line))) # => "I've decided to tackle it"
yumaikasPlayground
(os/shell "echo bar > /tmp/foo")
(with
  [file-handle
   (file/open "/tmp/foo")
   (fn [fd] (file/close fd))]
  (file/read file-handle :all))  # => @"bar\n"
cellularmitosisPlayground