-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the concept of NULL #70
Conversation
6508714
to
b14ded5
Compare
@@ -12,6 +12,8 @@ def initialize(token) | |||
def value(context={}) | |||
v = context[identifier] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to see if the context
has the key identifier
here, rather than checking the to see if the returned value is nil
. That way, we could explicitly pass in nil
as an identifier value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is definitely cleaner, and I agree that relying on a special string 'null' as an input is strange. The problem with the way things are written at the moment is that context
has the unbound variable's key in it whether or not it was passed in as an input, because of the way that the context hash is built up here https://github.com/rubysolo/dentaku/blob/master/lib/dentaku/bulk_expression_solver.rb#L46 ... actually looking at that now, that looks like an avoidable behavior. Be right back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, I don't see a way to avoid that behavior while keeping the recursive algorithm. So the behavior there always sets a key on the context.
It seems to me that one path forward is to build in checks for nullity into all the relevant operators, so that you would throw the unbound variable error when performing a +
, -
, /
etc. That seems like a pain and complicates things, but I think it might have some benefits, like allowing an IF
statement where the right side is irrelevant so it doesn't matter if its variable was passed or not. I won't work on that unless you think that is a good idea though.
b14ded5
to
959339c
Compare
@rubysolo Decided to go for something simpler for this PR. In the original code there were two parts within the commit:
|
Allow NULL to be referenced in formulas, which will be evaluated as nil.
This requires moving things around so that nil values are only set to the nodes' context hash if that value was explicitly set on the calculator. Previously every referenced variable name would be added as a key to the context hash even it was unbound.
9cceefa
to
1058003
Compare
Came up with another commit for 2) which passes all tests. The change is a little subtle but I can't see anything wrong with it. |
Thanks, this is a good addition! I took |
@rubysolo What do you think about releasing another gem for this change? It would help us with managing our dependencies since right now we have to point at the sha in multiple gemfiles instead of in a single gemspec. |
Done! |
Thanks :) |
but the implemented |
I would understand if you're not interested in this change. We need it because we want to have a function like this:
... where the idea is that you only add up the values that are numeric. The reason that some charges are strings is because we are currently passing in the value of those variables as the string 'null'. This is because if the values of these variables was actually nil then the calculator would raise an unbound variable error before entering this function. And while this works, it's not great because we still have to pass back another string afterwards, requiring lots of
blah != ''
checks throughout our calculations.