lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Apr 6, 2010 at 3:58 AM, steve donovan <steve.j.donovan@gmail.com> wrote:
> On Tue, Apr 6, 2010 at 9:41 AM, Pan Shi Zhu <pan.shizhu@gmail.com> wrote:
>> does it provide any advantage calling assert(loadfile(...))()  instead
>> of dofile() ?
>
> It does have different semantics - the first form throws an exception,
> the second one is protected. To get the same semantics, you need more
> checking, so dofile() is best.

This isn't quite true. dofile is not protected from errors during
load. Apparently the only functional difference between the two code
snippets is the stack traceback:

batrick@neverwinter:~$ lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> assert(loadfile("blah"))()
stdin:1: cannot open blah: No such file or directory
stack traceback:
	[C]: in function 'assert'
	stdin:1: in main chunk
	[C]: ?
> dofile("blah")
cannot open blah: No such file or directory
stack traceback:
	[C]: in function 'dofile'
	stdin:1: in main chunk
	[C]: ?


>> we probably need a dostring() or eval(), instead of remove the dofile().
>
> We used to have a dostring(), but it's an easy function to write.

Functionally dofile can be equivalently replaced with:

function dofile (name)
  local f, err = loadfile(name)
  if not f then error(err) end
  return f()
end

-- 
- Patrick Donnelly