"Hiding" the mixin keyword?
Reiner Pope
xxxx at xxx.xxx
Fri Feb 23 15:04:33 PST 2007
I don't think this is possible, but it certainly seems worthwhile to
support more advanced features through a standard function-call interface.
Andrei has been nagging Walter about this and making suggestions, so I
suppose we can hope it will be implemented sometime soon.
renoX wrote:
> What I would like is a way to 'hide' the mixin keyword, is-it possible
> to put into the template so that the result would look like:
> putf!("Should be 31: %d{x+y+1}\n"); ?
I don't think it would be too hard to work out the details. What I can
think you might need is the following:
1. The ability to specify that certain parameters in the list must be
compile-time constants, and the ability to overload by this.
2. Some kind of interface at the declaration site that specifies that
the return value of a given function be mixed into the code instead of
just treated as a value
The first one could look like this:
>
> RegExpMatch matchRegexp(static char[] compileTimePattern, char[] text){...} // Parses the pattern at compile time
> RegExpMatch matchRegexp(char[] runtimePattern, char[] text) {...} // Parses it at runtime
This would mean a library user wouldn't have to call a different
function to get the compile-time version than to get the runtime version.
The second would just involve adding some kind of modifier to a template
declaration, like the following:
> mixin template putf(A...)
> {
> const char[] putf = ...;
> }
>
> // Instantiated like:
>
> putf!("stuff");
>
> or
>
> mixin char[] putf(static char[] pattern)
> {
> ...
> return generatedCode;
> }
>
> // Instantiated like:
>
> putf("pattern %d{foo}");
>
> // with the return value mixed in
This could also give you cool things, like the ability to mimic tuple's
compile-time indexing and make your own tuple wrapper:
>
> struct MyTuple(A...)
> {
> A data;
> typeof(A[i]) opIndex(static size_t i)
> {
> return data[i];
> }
> }
Cheers,
Reiner
More information about the Digitalmars-d-learn
mailing list