Running "sum()" on empty Pool gives a TypeError in v1.0.0
Closed this issue · 2 comments
I'm upgrading the calculator to icepool 1.0.0 (congratulations on the release!).
I've run into the following issue (very easy to work around, but I'm wondering if it's intentional).
>> Pool([]).sum()
TypeError: None is not a valid final outcome. This may have been a result of not supplying any generator with an outcome.
This happens because for each attack roll result, I use icepool to calculate the damage done, and sometimes, sometimes there are no saves to take.
Thank you!
Glad to hear from you!
This is sort of intentional, if unfortunate. While dice usually have integer outcomes, Icepool allows arbitrary outcome types, so they could also be strings, tuples, icepool.Vector
s, etc. Previously I had Pool.sum()
simply only support integers, but I decided to make it generic so any outcome type that supports addition and multiplication can be used. The downside of this decision is that an empty Pool
has no outcomes and therefore no outcome type, so now it is not possible to determine whether the result should be 0
, ''
, ()
, Vector([0, 0, 0])
, or so forth.
In your particular case, you could sum the dice directly without going through a pool:
r = Pool(
[dSave for x in range(saves)] + [dCrit for x in range(crit_saves)] + [dPlasma for x in range(plasma_saves)]
).sum()
is the same as
r = saves @ dSave + crit_saves @ dCrit + plasma_saves @ dPlasma
Great, I see now that without knowing the zero element you cannot return zero, makes sense, and a worthwhile trade off for a more generic Pool
class.
Thank you for the code suggestion, a very elegant solution!