Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
class FileInfo
constructor: (filename) ->
@name = filename
mtime: ->
# in CoffeeScript, the last thing in a function is returned
# which in this case is the value returned from @withFile
# which is the value returned from the inner function
# => binds scope to the correct `this`
@withFile (f) =>
mtime = @mtimeFor(f)
return "too old" if mtime < - 1000
console.log "recent!"
return mtime
body: ->
@withFile (f) =>
mtimeFor: (f) ->
return File.mtime(f)
withFile: (fn) ->
f =, "r")
fn(f) # the result of this is implicitly returned in CoffeeScript
function FileInfo(filename) { = filename;
FileInfo.prototype = {
mtime: function() {
// return the result of the inner function
return this.withFile(function(f) {
var mtime = this.mtimeFor(f);
if (mtime < - 1000) {
return "too old";
return mtime;
}.bind(this)); // bind scope
body: function() {
return this.withFile(function(f) { return; });
mtimeFor: function(f) {
return File.mtime(f);
withFile: function(fn) {
try {
var f =, "r");
return fn(f); // call and return the result of the "block"
} finally {

It didn't take much work to get JS to perform as expected, a few extra return statements and a bind call and we're done. CoffeeScript is even closer with its implicit returns and => binding syntax. Am I missing something else, or do we already have everything we need and this is just sugar?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.