What type functions mean on a language level

Jacob Carlborg doob at me.com
Sat Jan 2 08:47:07 UTC 2021


On 2021-01-02 01:05, Stefan Koch wrote:

> Also I am not sure how I want the api to look like.
> 
> Perhaps like this?
> struct StructField
> {
>      __type__ type;
>      string name
> }
> __type__ makeStruct (string name, StructField[] fields)
> {
>      __struct__ sresult = __interal__magic__makeStruct(name);
>      foreach(f;fields) { sresult.addField(f.name, f.type); }
>      return __internal__magic__registerType(sresult);
> }

I'm not sure if it needs to have an API. Just look at how Zig does it.

__type__ LinkedList(__type__ ElementType)
{
     return struct {
         static struct Node
         {
             ElementType element;
             Node* next;
             Node* prev;
         }

         Node* first;
         Node* last;
     }
}

LinkedList(int) list;

If you want to pass in the field names, I guess you'll have to resort to 
string mixins. Sure, there could be better alternatives than string 
mixins, but I think that's a separate issue.

> See the sketch of a possible API above.
> 
> And feel free to suggest your own!

Here I've experimented in exposing the compiler AST [1].

[1] 
https://github.com/jacob-carlborg/druntime/blob/517dafcf54ad73049fb35d1ed5fa2ad6619b9ac4/src/core/ast/expression.d

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list