Metaprogramming without templates

Stefan Koch uplink.coder at googlemail.com
Wed Jun 23 21:06:23 UTC 2021


On Monday, 21 June 2021 at 14:15:27 UTC, SealabJaster wrote:
> On Monday, 21 June 2021 at 13:18:21 UTC, Stefan Koch wrote:
>> ...
>
> Exciting, even if the end code looks like a complete hack!
> [snip]
>
> ```sql
> SELECT * FROM people WHERE age >= $1;
> ```
>
> And it will then automatically forward whatever's in `age` into 
> parameter $1.


You can now get the Variable declaration as reflection parameter.
also you can now pass scopes ;)
Look at the following code:

```D
import core.reflect.type;
import core.reflect.node;
import core.reflect.decl;

int[2] myArr;

static immutable arr_node = nodeFromName("myArr");
static immutable arr_type = cast(immutable TypeArray) 
(cast(immutable VariableDeclaration)arr_node).type;
static assert(
     arr_type.kind == "sarray" // this name is subject to change.
     // TypeKinds are supposed to be an enum not a string but I 
didn't feel like implementing it yet :)
     && arr_type.identifier == "int[2]"
     && arr_type.nextOf.identifier == "int"
     && arr_type.dim == 2
);

pragma(msg, arr_node);
```
this will output:
`VariableDeclaration("myArr", [], 0, "", TypeArray("sarray", 4u, 
8u, "int[2]", Type("int", 4u, 4u, "int"), 2LU))`
where VariableDeclaration is defined as
```D
class Declaration : Node
{
     string name;
     Node[] attributes;
     Linkage linkage;
     string comment;

     abstract immutable DeclarationKind kind() pure;
}

class VariableDeclaration : Declaration
{
     Type type;
     override final immutable DeclarationKind kind() pure { return 
DeclarationKind.VariableDeclaration; }
}
```
     type is defined as:
```D
class Type : Node
{
     string kind;
     uint alignSize;
     ulong size;
     string identifier = null; /// optional Types may be anonymous
}

class TypeNext : Type
{
     Type nextOf;
}
class TypeArray : TypeNext
{
     int dim;
}
```
     so this reads:
```JS
name: "myArr",
attributes: [],
Linkage: 0, //(Linkage.init) we don't capture it yet
comment: "",
type: {
   kind: "sarray",
   alignSize: 4
   size: 8,
   identifier: "int[2]",

   nextOf: { // element type
     kind: "int",
     alignSize: 4,
     size: 4,
     identifier: "int"
   },
dim: 2
}
```

I am rather happy with the structure of the output actually.
The implementation is such much cleaner than type functions are 
as well ;)

Let me know what you think.

P.S. I do apologize for this very long post, I don't really know 
how to present this work in progress stuff properly.


More information about the Digitalmars-d mailing list