www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature Request: The ability to deduce delegate parameters based

Hash: SHA1

So I was chatting away happily in #d, when the discussion came upon an
algorithm to map an array onto (element, predicate(element)) tuples,
then select all tuples that have the same predicate value as the first
This being a task well suited for functional programming, I tried to
whip up a snippet in a modified form of the scrapple.tools.ext/iter
syntax, and came up with this:

~tszip ~ (T elem) { return pred(elem); } ~ filter ~ ((TupleStruct!(T,
typeof(pred(T.init))) elem, typeof(pred(T.init)) *first) { if (!first) {
first=new typeof(first); *first=elem.values[1]; return true; } else
return elem.values[1]==*first; }~tfix(null)) ~maps!("_.values[0]")

My point here being the definition of the delegate that's being passed
to filter, specifically
(TupleStruct!(T, typeof(pred(T.init))) elem,
	typeof(pred(T.init)) *first)

Interestingly, all of this type information could be determined
implicitly by the opCat function it's being passed to, shortening the
definition to
(auto elem, typeof(pred(T.init)) *first)

This doesn't break any existing code, since it merely expands
functionality, not changes it. The only downside I could see is that it
might set a precedent wrt. overloading by assignment. Also, the use of
such auto delegates would have to be limited to cases where the intended
type is unambiguous. But even with those limitations, this form of
delegate definition would still be useful for cutting down on type
What do you think?
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

Sep 04 2007