June 16, 2015 Re: static foreach considered | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel N | On Tuesday, 16 June 2015 at 20:39:44 UTC, Daniel N wrote:
> Or is there a better way to accomplish the above?
>
> Daniel
Sorry for the noise, ofc there's a better way...
struct weird(T)
{
static if(T.sizeof<=8)
{
void proto_val(T val);
alias U = ParameterTypeTuple!proto_val;
}
else
{
void proto_ref(const ref T val);
alias U = ParameterTypeTuple!proto_ref;
}
void zzz(U x)
{
}
}
Daniel
|
June 16, 2015 Re: static foreach considered | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 8 June 2015 at 20:02:11 UTC, Andrei Alexandrescu wrote:
> I'm trying to collect together motivating examples and to figure out the semantics of the feature.
I was just playing around with something and I thought being able to do a loop over an enum at compile time would be convenient. The motivation is when you have a function that works a little differently for arrays than for scalers, but it works the same way for all sizes of arrays (i.e. 1d/2d/3d all the same).
template GenArrayMathFunction(string function_name, string array_string)
{
const char[] GenArrayMathFunction =
"real" ~ array_string ~ " " ~ function_name ~"(real" ~ array_string ~ " x) {\n" ~
"\treal" ~ array_string ~" result = x.map!(a => " ~ function_name ~ "(a)).array;\n" ~
"\treturn result;\n" ~
"}";
}
private enum array_dimension
{
Dim1 = "[]",
Dim2 = "[][]",
Dim3 = "[][][]"
}
static foreach(i; array_dimension)
{
mixin(GenArrayMathFunction!(f, i));
}
Basically, the first part generates a string representing a function that uses map to apply a function to some input. The type of the input depends on a string representing the kind of array. The enum represents a string for 1/2/3-dimensional arrays. Finally, I would want to loop through the enums and use mixin to create a function for each.
The foreach loop doesn't work right now. Right now, I've just written out each of the three functions and put in array_dimension.Dim1, etc.
|
June 17, 2015 Re: static foreach considered | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel N | On Tuesday, 16 June 2015 at 21:21:26 UTC, Daniel N wrote:
> On Tuesday, 16 June 2015 at 20:39:44 UTC, Daniel N wrote:
>> Or is there a better way to accomplish the above?
>>
>
Yet another snippet.
static foreach(param; ParameterTypeTuple!((ref int _1) {}))
{
// in my dream it remains 'ref int', doesn't decay into 'int'.
void fun(param p) {}
}
|
May 14, 2018 Identifier from string - Re: static foreach considered | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Monday, 8 June 2015 at 21:32:52 UTC, Timon Gehr wrote: > On 06/08/2015 10:16 PM, Idan Arye wrote: >> It would be nice together with this feature to be able to mixin >> identifiers: >> >> static foreach (ident; ["foo", "bar"]) >> { >> auto mixin(ident)() >> { >> // code for foo/bar >> } >> } > +1. Other use cases: > > auto mixin(ident) = x; > > identifier.list.mixin(ident).foo(); > > import std.mixin(ident); Browsing bugzilla, I've discovered Idan's feature was actually proposed in 2009, but with different syntax: int __ident(name)() {...} This seems better syntax, see: https://issues.dlang.org/show_bug.cgi?id=2698#c3 |
Copyright © 1999-2021 by the D Language Foundation