D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6043 - Chainable template mixin
Summary: Chainable template mixin
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-21 16:43 UTC by bearophile_hugs
Modified: 2017-10-18 15:28 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-05-21 16:43:54 UTC
I suggest to add something like this to D2 Phobos (this is D1 code):

template Chainable() {
        Chain!(typeof(this), Range) opCat(Range)(Range r) {
            return chain(this, r);
        }
}


Then in the body of a lazy range both Phobos ones like map, filter, zip, iota, and so on, and user-defined ones, you may add:

mixin Chainable;


This allows to write more natural and readable code like:
iota(5) ~ filter!q{a}([1,0,3])
map!abs([1,2,-5]) ~ [10, 20]

instead of more noisy:
chain(iota(5), filter!q{a}([1,0,3]))
chain(map!abs([1,2,-5]), [10, 20])

See also bug 5638 for an useful optimization.
Comment 1 Andrej Mitrovic 2013-01-22 18:18:20 UTC
Very interesting idea!

In fact, it's a bit odd that we allow the generally slow array appends (due to allocations, reallocations, etc), but we use functions for chaining ranges.

I wonder if Andrei would approve this. Maybe you should bring this up to the newsgroups for better awareness.
Comment 2 RazvanN 2017-10-18 15:28:56 UTC
There was an attempt at getting this in phobos, that was rejected. For more information see the newsgroup post [1] and the interesting discussion in the PR [2]. Closing as WONTFIX.

[1] http://forum.dlang.org/post/bcazbwztlspwzpvlrblm@forum.dlang.org
[2] https://github.com/dlang/phobos/pull/3353