You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
None#nil? answers true — documented in #25 as a deliberate compromise so that "AR internals and ordinary nil checks treat an absent value as absent". But Array#compact, the single most common nil-dropping idiom in Ruby, does not drop it:
[Some(1),None()].compact# => [Some(1), None] expected [Some(1)][Some(1),None()].flat_map(&:to_a)# => [1] the working idiom
Tested on errgonomic 0.8.0.
Cause
Array#compact is implemented in C and tests for the actual Qnil object rather than calling nil?. So the None#nil? == true compromise, which is what makes ordinary nil checks work, stops short exactly where a collection is involved.
Why it matters
This is silent, and it lands on a documented convention. In our Rails app the action-tree layer's README teaches this exact shape for declaring sub-actions:
Those are all optional: true belongs_to, so under ActiveRecordOptional every one becomes an Option and .compact silently keeps the Nones. Downstream consumers then dereference them:
a recursive step collector guarded by return [] unless action — None is truthy, so the guard passes and action.steps raises
9+ models in our codebase use .compact in exactly this position, so this will recur on nearly every action model we convert.
The asymmetry is the trap: if x.nil?, unless x.nil?, and x.nil? ? a : b all behave as #25 promises. [..].compact looks like the same check and does not.
Suggested fix
I don't think this is fixable in Array#compact itself without patching core, and patching Array#compact globally would be a much larger blast radius than this is worth.
Two things that would help instead:
Document it alongside the None#nil? compromise in Document the ActiveRecord pragmatic compromises in ActiveRecordOptional #25 — the compromise's stated purpose is "ordinary nil checks keep working", and it's worth naming the one very common check where that isn't true, with flat_map(&:to_a) as the replacement.
Summary
None#nil?answerstrue— documented in #25 as a deliberate compromise so that "AR internals and ordinary nil checks treat an absent value as absent". ButArray#compact, the single most common nil-dropping idiom in Ruby, does not drop it:Tested on errgonomic 0.8.0.
Cause
Array#compactis implemented in C and tests for the actualQnilobject rather than callingnil?. So theNone#nil? == truecompromise, which is what makes ordinary nil checks work, stops short exactly where a collection is involved.Why it matters
This is silent, and it lands on a documented convention. In our Rails app the action-tree layer's README teaches this exact shape for declaring sub-actions:
Those are all
optional: truebelongs_to, so underActiveRecordOptionalevery one becomes an Option and.compactsilently keeps theNones. Downstream consumers then dereference them:return [] unless action—Noneis truthy, so the guard passes andaction.stepsraisesall_actions.map { |a| a.examine(prefix) }— unguarded, raises9+ models in our codebase use
.compactin exactly this position, so this will recur on nearly every action model we convert.The asymmetry is the trap:
if x.nil?,unless x.nil?, andx.nil? ? a : ball behave as #25 promises.[..].compactlooks like the same check and does not.Suggested fix
I don't think this is fixable in
Array#compactitself without patching core, and patchingArray#compactglobally would be a much larger blast radius than this is worth.Two things that would help instead:
None#nil?compromise in Document the ActiveRecord pragmatic compromises in ActiveRecordOptional #25 — the compromise's stated purpose is "ordinary nil checks keep working", and it's worth naming the one very common check where that isn't true, withflat_map(&:to_a)as the replacement.Array#compact_options(or similar) in thecore_extset added by Optional collections: OptionalHash, OptionalArray, and additive lookups #38, so there's a named idiom to reach for rather than the somewhat crypticflat_map(&:to_a).