Re: Changes in the ESL papers

new topic     » goto parent     » topic index » view thread      » older message » newer message

Derek Parnell wrote:
> 
> Jason Gade wrote:
> > 
> > D. Newhall wrote:
> > >
> > > Juergen Leuthje wrote:
> > > > - What shall functions return in case of an error?
> > > Depends, but usually 0 or -1. If you look at match() and find() they
> > > return 0 on failure
> > > because those aren't valid return values but open() returns -1 for failure
> > > (since 0
> > > means standard input I'm guessing). Also, stuff like get() and value() can
> > > return 0
> > > as a valid result so they return a sequence starting with an error code
> > > declared as
> > > a constant. I think this is a case-by-case issue for the most part.
> > 
> > As long as routines don't return a sequence or an atom based on *whether*
> > there is
> > an error.  This is a behavior of Euphoria standard routines that I hate --
> > users shouldn't
> > have to declare variables as object in order to differentiate between a
> > valid return
> > and an error.
> 
> For what it's worth, I've tended to adopt the convention of *always* returning
> a sequence.
> The first element is either 0 (an error) or 1 (a success), and the second is
> the ancillary
> data. In the case of an error, it is something that helps users know what sort
> of error
> occurred, and in the case of success, it is the returned data from the
> function.
> 
> This keeps code consistant and extensible. It also encourages (forces?) coders
> to take
> notice of error codes because it is no longer possible to do ...
> 
>    if func() = 0 then ...
> 
> Instead you must do ...
> 
>    rc = func()
>    if rc[1] = 0 then ...
> 
> However, I guess most coders won't bother to adopt this discipline. It is
> similar to
> the RDS value() function's return.
> 
> -- 
> Derek Parnell
> Melbourne, Australia
> Skype name: derek.j.parnell
> 

I understand the rationale and I don't have a better solution, but for some
reason it just seems inelegant.

if func()[1] = -1 then
 --
end if

looks ugly too...

I guess my complaint is more towards the value() function itself. It doesn't
adhere to the principle of least surprise. I would expect it to return an atom of
'0' or call an error function on error. It adds an extra step to parsing a
number. Routines that are expected to return a sequence I don't have a problem
with.

=====================================
Too many freaks, not enough circuses.

j.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu