On 27 April 2017 at 14:14, Andy Jones <Andy.Jones / jameshall.co.uk> wrote:

> I generally try to avoid propagating nil all through my code precisely
> because of the problems it causes.  Really you are better off catching that
> nil value and dealing with it in one place, rather than having to deal with
> it in all the places it could later turn up.
>

The problem with this approach is that the nil needs to propagate through
the equations, bailing on the first exception is not viable - tried it.
Currently I would have to wrap nearly 2,000 calculations in rescue blocks.
Not impossible as the final code comes from a code generator but means that
the code would become totally unreadable (not that 2,000 lines of code are
that easy to read).

Patching Float and NilClass is the cleanest solution so far, the next most
would be to grab the syntax tree and render "nil + 12.0" as "my_add(nil,
12.0)" where my_add is a method that checks the arguments and behaves
accordingly.



> Here's a weird idea: if you really have a need for this, why not make your
> own nil class specifically for values that need this functionality?


Actually that sounds like a really good idea :)

I will give it a go (but will that cope with "12.0 + nil" ?)
(supressed text/html)
Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>