Type inference on guarded alternative #759
-
Hey guys, I'm wondering how the type of a parse rule with guard is inferred. I found the following thing: Modify the arithmetic example's rules as follows:
A new kind of primary expression There are workarounds for this, but I think that this behavior is counterintuitive, is there something that I'm missing about guarded alternative? Or is this case too hard for type inference? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So in short, the type inference completely disregards alternative/group guards. I'm kind of curious what you are expecting as a generated type. The main issue here is that rules are transformed in isolation and not in the context of their calling rule. Especially since seem to expect that a rule is transformed into two distinct types, which is not really something which the current system is able to accomplish. And I believe this would be very hard to handle from a user perspective, as the amount of generated types is type Expression<next extends boolean> = If<
next,
NextExpression | NumberLiteral | ..., // Keep `NextExpression` if `next` is true
NumberLiteral | ... // omit the `NextExpression` if `next` is false
>; However, that would lead to a few other issues, as we cannot determine during runtime which type it is, so the generated |
Beta Was this translation helpful? Give feedback.
So in short, the type inference completely disregards alternative/group guards. I'm kind of curious what you are expecting as a generated type. The main issue here is that rules are transformed in isolation and not in the context of their calling rule. Especially since seem to expect that a rule is transformed into two distinct types, which is not really something which the current system is able to accomplish. And I believe this would be very hard to handle from a user perspective, as the amount of generated types is
2^n
withn
being the number of guard arguments in a rule. The only thing which I could reasonably imagine would be akin to something like: