ch-ch-changes

Sean Kelly sean at invisibleduck.org
Wed Jan 28 12:23:34 PST 2009


== Quote from grauzone (none at example.net)'s article
> One thing about std.algorithm: you really seem to like using
> compile-time strings as literals. However, this makes the use of
> delegates harder. For example, to use a delegate, you need to do this
> (quoted from your docs):
>  > int[] a = ...;
>  > static bool greater(int a, int b)
>  > {
>  >     return a > b;
>  > }
>  > sort!(greater)(a);  // predicate as alias
> In my opinion, doing something like
>  > sort(a, (int a, int b) { return a > b; });
> would be simpler and more intuitive than passing a delegate name as
> template parameter. (The delegate literal syntax still could be
> improved, though.)

One issue with delegates is that they don't work in compile-time code.
I found a way around this in Tango by using structs with opApply, but
it still tosses out the ability to use delegate literals for predicates, which
is kind of annoying.  Another issue is that delegates are currently much
slower than using the alias approach because DMD doesn't inline delegates,
even when it theoretically could.

I'd considered the idea of providing overloads in tango.core.Array which
allow the alias approach to be used, but D1 support for this just wasn't
there the last time I looked into it.  Also, this would work by converting
the aliases to delegates, which isn't ideal from a performance perspective.

In short, if the target is D2 then I think the alias approach is the correct
one.  Delegates are more portable across versions, but I don't think they
offer much over aliases in terms of functionality.

> Another problem is, that using string mixins seems to be quite
> problematic, because they are mixed in in a different scope. If I'm not
> mistaken, you can't do this:
>  > int foo(...) {...}
>  > sort!("foo(a, b);");
> You might think that "sort!("a>b", a);" is elegant and short, but this
> probably only works out in toy-examples.

I'm pretty sure that nested functions can be aliased in D2 (and perhaps
in D1 now as well).  This may not be quite as succinct as using delegate
literals, but I wouldn't use delegate literals for complex comparisons anyway.


Sean



More information about the Digitalmars-d mailing list