extend foreach to work on non-arrays
IntegratedDimensions
IntegratedDimensions at gmail.com
Thu May 24 23:22:56 UTC 2018
On Thursday, 24 May 2018 at 23:02:00 UTC, Neia Neutuladh wrote:
> On Thursday, 24 May 2018 at 22:43:00 UTC, IntegratedDimensions
> wrote:
>> Doesn't make any sense?
>>
>> foreach(a; x)
>>
>> if x is not an array then a = x and the loop reduces simply
>> and function to the case it is not so their can be no harm.
>>
>> It unifies the concepts so that one does not have to worry if
>> x is an array or not and can offer no harm since when x is not
>> an array everything simply reduces to an an alias of x.
>
> You can already do this for any type you define:
>
> class Foo
> {
> auto iterate() { return only(this); }
> alias iterate this;
> }
>
> Can you give examples of code that is awkward today that would
> be simplified with your proposal? I don't expect people to give
> full cost-benefit analyses for every suggestion, but it'd be
> nice if you could at least mention some of the upsides.
The upside is that it is a composite pattern and does not require
any extra code to use preexisting functionality. There is no
downside, which is an upside.
foreach(a; x)
{
// uses a
}
gets directly converted to
{
// uses x
}
by a simple renaming/aliasing of a to x and so it cannot require
any more issues than what already exists.
Your method requires one to have access to all type definitions,
which is not the case.
I do not know why I would be required to specify a use case to
justify the usability. Most things are not usable until they are
invented. Most people did not know how useful the hammer would be
until someone created it. You have to build it for them to come.
I have my use cases which is why I came up with the idea in the
first place. I needed a hammer but no one invented it. If I give
you my use case then there would be two main outcomes: You
attempt to find a workaround for the use case or claim that it is
not applicable. These are the "I have a rock that should work as
good as that hammer thingy you want" and "Hammers are useless".
If you can't find validity in the suggestion based on it's own
inherent usefulness than the only way I can convince you is to
provide examples that you actually find useful... that makes it
difficult on my part and is not fair to me.
More information about the Digitalmars-d
mailing list