[Solved] Syntax plugin - Comparing types


#1

Syntax plugin seem to have a lot of missing docs, so I’m wondering whether it’s possible to write something akin to this logic using syntax plugins:


   entity! Parent {
        key: int,
   }

   entity! Child {
       key:int,
       parent: Ref(int, Parent.key),
   }

And have it so Ref(int, Parent.key) type can check during compile time whether Parent has a type called key and if its type matches parent (e.g. Ref(str, Parent.key) or Ref(int, Parent.doesnt_exist) fail) ? Would it be doable if I wrote everything inside a giant syntax extension like:

    dsl! {
        entity Parent { ...}
        entity Child {...}
    } 

I assume from my reading that only latter is possible and that you can’t get type in Syntax extensions, unless they are lint, correct?


#2

Yes.

A syntax extension only has access to the syntax of its arguments; type information does not yet exist when it gets run.

However, a lint is unnecessary for doing this check: assuming you generate code that in some way accesses Child from Parent (or vice versa), the compiler should abort on a type error or invalid field/method access before it even gets to running lints.


#3

That’s a very clever point, although I fear the errors produced this way would be harder to understand.