lua-users home
lua-l archive

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



I was expecting that assert mention! :)

nsfm:fileExistsAtPath(testfile)
    or error "File doesn't exist!"
  vs.
assert( nsfm:fileExistsAtPath(testfile),
        "File doesn't exist!" )

It's basically a matter of taste, the perlish way has imho many things going for it:

- focus is on what's being called (the function is upfront, not 'assert') - this already is luaish, too, "a or b or .." is a known idiom for values - assertion can be switched on/off by --'ing the latter line (assert needs paranthesis)

Just my poll, of other people's opinions. thanks for yours!

-asko


Rici Lake kirjoitti 1.1.2007 kello 5.36:


On 31-Dec-06, at 9:56 PM, askok@dnainternet.net wrote:


A perl-ish way of making error checking, but neat and effective:

_= nsfm:fileExistsAtPath(testfile)
    or error "File doesn't exist!"

_= nsfm:copyPath( testfile, newfile )
    or error "File copy failed!"

Currently, Lua does not allow leaving the "_=" away, since that would make these commands and commands don't have a value ('or' gives this a value).

In my opinion, evaluation results could be allowed even if the value is not stored anyway. In a way, they would have a "drain" that eats up the value.

I do know that by a function this can be overcome, but the neaty is to have the essential thing first (the command), and ability to switch on/off the error check by simply commenting that out. Anyways, this is caused by my exposure to perl lately, it does show. Perl has a lot of those thisandthat or die things.. Which I actually like(d).

But what's wrong with:

assert(nsfm:fileExistsAtPath(testfile), "File doesn't exist!")

Or even, if fileExistsAtPath follows normal Lua conventions of
returning <nil/false, error message>,

assert(nsfm:fileExistsAtPath(testfile))

?

Another (perhaps warped) possibility is:

repeat until nsfm:fileExistsAtPath(testfile)
   or Shriek "File doesn't exist"