Partially instantiating templates?
Magnus Lie Hetland
magnus at hetland.org
Mon Jan 31 05:47:27 PST 2011
On 2011-01-31 12:55:07 +0100, Simen kjaeraas said:
> ElementType!Range minArg( alias fun, Range )( Range range, out
> ReturnType!fun ) {
> ...
> }
Aaaah. I guess I tried ElementType(Range), forgetting to make it a
compile-time parameter. Thanks. (Hadn't seen ReturnType; makes sense :)
> Might I also ask why you use an out parameter instead of a tuple return?
Well... I had a tuple return at first, but one of the advantages of
returning multiple values that I'm accustomed to is the ability to
assign to multiple variables, such as
arg, val = minArg(...)
(Yeah, I'm a Python guy... ;)
As far as I can see, you can't do that here? Using result[0] and
result[1] or the like, or assigning separately to two variables just
seemed more cumbersome. Then again, I could use a tuple with named
members, I guess. In your opinion, what would be the prettiest (in D
terms) way of doing this?
[snip]
> D currently does not support template currying to any good degree.
OK. Well, I guess I don't really need it. Still trying to get a feel
for what's "normal" D :)
> However, there is at least one library out there that does that for you:
>
> http://www.dsource.org/projects/dranges
>
> In the file templates.d, there is the template CurryTemplate, which
> rewrites a template to a set of nested templates. This would allow you
> to partially instantiate a template, and add more parameters as you go.
I see. I actually don't mind writing nested templates myself -- but for
some reason I couldn't get them to work properly. (D kept complaining
about declarations vs instances, and the like; I guess I'll have a look
at how dranges does it.)
>> I've been able to make either one of these two pieces of functionality
>> work with some twiddling and nesting (i.e., *either* instantiating
>> optArg into minArg/maxArg, *or* instantiating explicitly defined
>> minArg/maxArg into specialized functions) but combining them has so far
>> eluded me (unless I start fiddling with strinc constants and mixin(),
>> which seems excessively hacky for such a simple thing).
>
> dranges' templates.CurryTemplate should take care of some of your problems.
> Not sure if it will fix them all.
OK, thanks.
By the way, if you have suggestions for other more "D-like" ways of
encapsulating this functionality (basically a linear scan for an
element that yields a max/min value for a given expression), I'd be
interested to hear that too. The best way to solve a problem is often
to rephrase it :)
--
Magnus Lie Hetland
http://hetland.org
More information about the Digitalmars-d-learn
mailing list