-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Find Julep: issue with sentinel values #47
Comments
@timholy @StefanKarpinski Do you think that's something we should consider for 0.7? |
It would likely be a painful deprecation, but with efficient unions we could have |
Ref. JuliaLang/julia#24673 (comment). Best! |
@timholy Do you want this in 1.0? Now is the last chance to push for this change. ;-) |
It does seem like it may be a better approach. |
Yes, it would be much better not to use 0 as a sentinel value. Returning |
@timholy Would you have time to make a pull request? I'm pretty swamped with my 1.0 items already (but I can help reviewing it). BTW, something I realized is that for |
Yes, now that I reached a fixed point on the broadcasting code, I'll try to get to it. I haven't followed |
Great!
It doesn't exist yet, that was just a proposal for how we could handle dictionaries whose key type includes |
So... I feel a little heretical - but isn't It seems a little dodgy to not allow for certain keys for generic indexables in the generic case (there's also a sentinel |
Perhaps, but that complicates the API considerably and it's hard to imagine a case where |
In similar cases in my own code, I've taken to creating a singleton type for sentinel values (e.g. |
It's only unambiguous because you don't use |
I wonder if there's some parallel to get(Dict, Key, Default) = Value || Default
get(Dict, Key) = Value || throw(Error)
get?(Dict, Key) = Some(Value) || nothing |
I think there's a somewhat fundamental split between containers like arrays where the keys are known to be something simple like an |
I mean that it's unambiguous from an API standpoint - the singleton/sentinel type only has a single purpose/use case that it is explicitly defined for. The ambiguity problem arises when a user would be using a specific value for their own purpose, and then your API claims that value as a sentinel value. It seems to me that a commonly punned-upon singleton value like Of course, you can't force the user not to abuse your API's singleton/sentinel value, but IMO it'd be far less likely to get abused than something like |
Would it be incredibly annoying to deprecate the use of |
Yes, but it's not uncommon for very generic code to put completely arbitrary objects into dictionaries that may have been arrived at by non-explicit means like reflection on the program state. You cannot guarantee that a |
This is a well-articulated point, and I agree that there could be sentinel-value-dependent bugs in generic code regardless of which sentinel value an API selects. Generic code eventually boils down to type-dependent code, though, and at that point, it seems to me that a API-specific singleton sentinel value (e.g. For the record, I'm not actually very opposed to |
Definitely merits consideration. But I think that no matter what you picked, you could get the "disallowed" value used as a key if we support enough container types. All it takes is this: found_keys = OrderedSet()
i = findifrst(f, container) # if i is NotFound() this will later lead to trouble
push!(found_keys, i)
...
for k in found_keys
kindex = findfirst(iequal(k), found_keys) # ambiguous |
In #25472 I initially tried adding an optional |
I don't think that - and am not trying to argue that - the chosen sentinel value should/could be disallowed as an element/key of arbitrary containers. I realize now that that's been the central point of contention here, so I should have clarified that earlier, my bad. That wasn't even the kind of situation I was thinking about. Rather, I was thinking of the problems that arise in situations like: result = findfirst(f, container)
if some_application_specific_predicate(result)
do_something(result, container)
end where |
|
True. I feel that it's not too uncommon for external packages to adopt At this point, it seems as though the idea of an API-specific sentinel value has been given (perhaps more than) its fair due of consideration, so I happily concede to the |
@ararslan I think so. People don't necessarily anticipate that their code could be passed That said, the same problem would arise if we allow |
I've just realized |
Originally posted as comment on commit, suggested to open issue instead.
cc @nalimilan
Find Julep
Extract from section Issues Beyond the Scope of This Julep
findfirst(x, v)
returns 0 if no value matchingv
is found; however, ifx
allows 0 as an index, the meaning of 0 is ambiguous. One could returntypemin(Int)
orminimum(linearindices(x))-1
, but what ifx
starts indexing attypemin(Int)
?It would be nice if supporting non-1-based indices was in the scope of this Julep, and it might be better to address non-1-based arrays before their use becomes more widespread.
Note that using
first(linearindices(A))-1
as a sentinel value would be non-breaking for standard 1-based arrays.If A starts indexing at
typemin(Int)
, then returned sentinel value would wrap-around totypemax(Int)
(i.e. no error).The calling code could then test for the sentinel value in the same way:
To simplify, could introduce new function
sentinelindex
, and use this consistently instead:The text was updated successfully, but these errors were encountered: