Re: How do I assign attributes of a function to another function?

2021-11-05 Thread Stanislav Blinov via Digitalmars-d-learn

On Friday, 5 November 2021 at 06:19:16 UTC, Li30U wrote:

...e.g.
```d
// ...
mixin ("ReturnType /*...snip...*/ " ~ member ~ "()(Parameters! 
/*...snip...*/

```

Note the `()` before parameter list. This would make your member 
function a function template, for which attributes will be 
inferred by the compiler based on the calls you make in the 
function body.


Of course, making it a template like this makes it non-composable 
with that same type as you're only inspecting functions, so you 
wouldn't be able to do e.g. a Group!(Group!(A, 3), 4);


Re: How do I assign attributes of a function to another function?

2021-11-05 Thread Stanislav Blinov via Digitalmars-d-learn

On Friday, 5 November 2021 at 06:19:16 UTC, Li30U wrote:
I am creating a templated object that is a storehouse for a 
heap object and executes their methods and returns an array of 
results. With the help of a template, I want to achieve this, 
but I want to assign the same attributes to the function. How 
can one pass the attributes of a function to another function?


There's https://dlang.org/spec/traits.html#getFunctionAttributes 
. Or you could define your functions as function templates, 
letting the compiler infer attributes. Especially as it looks 
like you function's attributes may not be made the same as the 
ones you defer to: since you're allocating with the GC, your 
function cannot be @nogc even if a deferred one can.


How do I assign attributes of a function to another function?

2021-11-04 Thread Li30U via Digitalmars-d-learn
I am creating a templated object that is a storehouse for a heap 
object and executes their methods and returns an array of 
results. With the help of a template, I want to achieve this, but 
I want to assign the same attributes to the function. How can one 
pass the attributes of a function to another function?


```d
template Group(T, int objects)
{
struct Group
{
enum __length = __traits(allMembers, T).length;

T[objects] __objects;

this(T[objects] __objctor) @safe
{
__objects = __objctor;
}

static foreach (member; __traits(allMembers, T))
{
static if ( __traits(getVisibility, 
__traits(getMember, T, member)) != "private" &&
__traits(getVisibility, 
__traits(getMember, T, member)) != "protected")

{
static if (isFunction!(__traits(getMember, T, 
member)))

{
mixin ("ReturnType!(__traits(getMember, T, 
member))[] " ~ member ~ "(Parameters!(__traits(getMember, T, 
member)) args)

{
ReturnType!(__traits(getMember, T, 
member))[] results;

foreach (e; __objects)
results ~= e." ~ member ~ "(args);

return results;
}");
}else
static if (member != "Monitor")
{
mixin ("typeof(__traits(getMember, T, 
member))[] " ~ member ~ "()

{
typeof(__traits(getMember, T, member))[] 
results;

foreach (e; __objects)
results ~= e." ~ member ~ ";
return results;
}");
}
}
}
}
}
```