-
Notifications
You must be signed in to change notification settings - Fork 18
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
results: collections integration #179
Conversation
There are some dangers of overly generic code. Consider the situation where a function Granted, this is likely to produce another error within the body of the loop, but with sufficient use of type inference the error message may turn out to be quite distant from the root cause. Due to the way how generics are supposed to mixin symbols from their caller scope, there is an alternative way to provide this genericity as an opt-in. You can place the definitions in a separate module that users who are interested in this generic treatment of Results as collections can import in their code which calls the generic algorithms they want to use. Unfortunately, due to current issues such as the generic sandwitch, a solution like this would still be problematic in practice. |
That generic code is not in Result however? Ie the code presented here is quite simple. That said, "generic" here means converters, sequtils and other "functional"-style api:s that might become more popular now that iterators finally can be combined more naturally - these helpers in Result simply integrate Result with more parts of the language. |
I don't understand this remark. Clearly the problematic example that I have provided will be possible with the current code. |
Yeah, the sandwitch issue is messy so for a central type like this, I would probably avoid it. I would probably use separate modules for anything that brings in additional imports (say .. std/options integration or I/O) but not for things that depend only on the Result type itself - from a UX perspective, this is often what you want as a developer, ie you don't want something to pull in deps you end up not using, but the dead-code-elimination feature of nim means the small helpers are "harmless" and convenient to always have available. |
sure - what I meant was that the judgement of "what kind" of code to use with these features is on the user, the code inside the Result module is not "overly generic", ie it doesn't contain too-open generics that would accidentally have negative effects on surrounding code. and yeah, if someone forgets to dereference in the for loop, that should quickly become apparent - it's like when usesing combining functional helpers is an incredibly powerful technique in languages that support it well - it "fits" Result that way, though the lack of good functional support in Nim in general has so far hampered progress on this front. |
|
This set of helpers allows treating Result and Opt as collections of 0 or 1 item, allowing iterating over them and checking "membership" - such integration is useful in generic code which can then be generalised to handle more complex cases - the integration is most useful with Opt. One design tradeoff here is the "explicitness" of `items` vs `values` for `Result` - technically error and value are "equal" and therefore we shouldn't give preference to the value, but there exists a convenience argument to treat the value as the "default" and therefore define `items` / `contains` for `Result` as well - this PR chooses the more conservative and explicit approach - a more liberal version can easily be added later should motivating examples emerge.
This set of helpers allows treating Result and Opt as collections of 0 or 1 item, allowing iterating over them and checking "membership" - such integration is useful in generic code which can then be generalised to handle more complex cases - the integration is most useful with Opt.
One design tradeoff here is the "explicitness" of
items
vsvalues
forResult
- technically error and value are "equal" and therefore we shouldn't give preference to the value, but there exists a convenience argument to treat the value as the "default" and therefore defineitems
/contains
forResult
as well - this PR chooses the more conservative and explicit approach - a more liberal version can easily be added later should motivating examples emerge.