Timeline for Creating a List of objects of a particular type only
Current License: CC BY-SA 3.0
22 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 24, 2018 at 15:51 | comment | added | Johnbot | @t3chb0t to be fair I have read almost everything by Eric Lippert and Jon Skeet. They point out a lot of pitfalls and how to handle them gracefully. | |
Apr 24, 2018 at 15:47 | comment | added | t3chb0t | @Johnbot I stand corrected! I've been here for so long and you're the first person ever mentioning that :-| better late then never, thx for having patience with me ;-) | |
Apr 24, 2018 at 15:36 | comment | added | Johnbot |
@t3chb0t It's eager parameter validation and fully lazy sequence evaluation. It's the best of both worlds and it's exactly how it's done in System.Linq.Enumerable in your link. Validate parameters defer enumeration.
|
|
Apr 24, 2018 at 15:33 | comment | added | t3chb0t | @Johnbot ok, but then it's no longer lazy... it's half-lazy, or semi-lazy it's someting not worth the effort... besides a stack trace already contains the necessary information and tests should discover such mistakes... it's an interesting technique but very expensive and the claim that It's usually done that way (always in the framework) doesn't seem to be true because none of the linq extensions does that system.linq.enumerable | |
Apr 24, 2018 at 15:24 | comment | added | Johnbot |
It's usually done that way (always in the framework) because it's confusing if you get an IEnumerable from somewhere and you slap on some additional filters and then get an exception when you try to consume it. It should fail early so you can easily find the source of the error. See also codeblog.jonskeet.uk/2011/01/18/gotcha-around-iterator-blocks
|
|
Apr 24, 2018 at 15:18 | comment | added | t3chb0t |
Isn't that the whole point of creating lazy iterators? I'd never split it like that... waste of time ;-) in fact, I might even use the ?? exception expression and validate the argument inside the foreach loop foreach (var item in source ?? throw new ...)
|
|
Apr 24, 2018 at 15:16 | comment | added | Denis |
@t3chb0t it seems that iterators are way too lazy. They defer the whole method execution until the Enumerator.MoveNext() is requested.
|
|
Apr 24, 2018 at 15:15 | comment | added | t3chb0t | @Johnbot could you elaborate on that? It's a very strage theory. The first version was perfectly fine. I always do it like that... | |
Apr 24, 2018 at 13:53 | comment | added | Denis | @Johnbot updated the answer to reflect that, thanks! | |
Apr 24, 2018 at 13:53 | history | edited | Denis | CC BY-SA 3.0 |
added 166 characters in body
|
Apr 24, 2018 at 13:44 | comment | added | Johnbot |
Parameter validation and yield return doesn't mix well. To get immediate parameter validation you need to split the iteration part into a private (or local) implementation function: See Fiddle
|
|
Apr 24, 2018 at 12:46 | comment | added | t3chb0t |
@AaronM.Eshbach true, on the other hand I believe OP might not know the is and as operators (yet) so he did it with == ;-)
|
|
Apr 24, 2018 at 12:45 | history | edited | Denis | CC BY-SA 3.0 |
added 1 character in body
|
Apr 24, 2018 at 12:44 | comment | added | Aaron M. Eshbach |
@t3chb0t, I know it shouldn't matter if everything is crafted correctly, and I would personally use OfType , I just wanted to point out that the solution is technically different from the original code.
|
|
Apr 24, 2018 at 12:44 | history | edited | Denis | CC BY-SA 3.0 |
deleted 4 characters in body
|
Apr 24, 2018 at 12:39 | comment | added | t3chb0t |
@AaronM.Eshbach since OP is using classes based on the ILoadPattern so it doesn't really matter... I doubt he has any descendants of the JointsLoadingPattern - actually it never matters and if it does then there is something else wrong with the code. I have never had any case where simple OfType wouldn't do the job. I would worry if it didn't work and I would start to think about the design again.
|
|
Apr 24, 2018 at 12:37 | history | edited | Denis | CC BY-SA 3.0 |
added 913 characters in body
|
Apr 24, 2018 at 12:34 | comment | added | Denis | @AaronM.Eshbach good catch, updated the answer. | |
Apr 24, 2018 at 12:32 | history | edited | Denis | CC BY-SA 3.0 |
added 913 characters in body
|
Apr 24, 2018 at 12:12 | comment | added | Aaron M. Eshbach |
This is what I would do, but I would clarify that this will return all subtypes of JointsLoadingPattern as well, which I believe is different from what the OP was doing in their original post.
|
|
Apr 24, 2018 at 11:24 | vote | accept | vin | ||
Apr 24, 2018 at 11:23 | history | answered | Denis | CC BY-SA 3.0 |