[MLton-user] MLton.Finalizable.touch

Florian Weimer fw@deneb.enyo.de
Thu, 25 Aug 2005 16:07:18 +0200


I don't understand how touch works.  This code:

fun test2 (str : string) =
    let open MLton.Finalizable
	val x = new str
    in addFinalizer (x, fn s => print (s ^ ": finalizer\n"));
       (fn () => (print "invoking touch\n"; touch x))
    end

val _ = (print "before test 4\n";
	 let val t = test2 "test 4"
	 in print "before GC 4a\n";
	    MLton.GC.collect ();
	    print "after GC 4a\n";
	    t ();
	    print "before GC 4b\n";
	    MLton.GC.collect ();
	    print "after GC 4b\n"
	 end)

Prints the following:

before test 4
before GC 4a
test 4: finalizer
after GC 4a
invoking touch
before GC 4b
after GC 4b

In other words, the finalizer is run before the call to touch.  Is
this the expected behavior?  If yes, what is the purpose of the touch
function?