Here's an example use case:
pubconstError=union(enum) {
InvalidToken: InvalidToken,
ExpectedVarDeclOrFn: ExpectedVarDeclOrFn,
ExpectedAggregateKw: ExpectedAggregateKw,
UnattachedDocComment: UnattachedDocComment,
ExpectedEqOrSemi: ExpectedEqOrSemi,
ExpectedSemiOrLBrace: ExpectedSemiOrLBrace,
ExpectedLabelable: ExpectedLabelable,
ExpectedInlinable: ExpectedInlinable,
ExpectedAsmOutputReturnOrType: ExpectedAsmOutputReturnOrType,
ExpectedCall: ExpectedCall,
ExpectedCallOrFnProto: ExpectedCallOrFnProto,
// ...pubconstInvalidToken=SingleTokenError("Invalid token {}");
pubconstExpectedVarDeclOrFn=SingleTokenError("Expected variable declaration or function, found {}");
pubconstExpectedEqOrSemi=SingleTokenError("Expected '=' or ';', found {}");
// ...
}In this example SingleTokenError makes a type and returns it. So here we have a bunch of tagged union fields with a bunch of matching field names and types. Importantly for this proposal, the types can be found by doing field access on the tagged union: Error.InvalidToken results in both the union tag type and the payload type.
This proposal is to allow, for example, implicitly casting InvalidToken to Error. You could change this code:
*(tryresult_tree.errors.addOne()) =Error {
.ExpectedVarDeclOrFn=Error.ExpectedVarDeclOrFn { .token=token_index },
};to this:
*(tryresult_tree.errors.addOne()) =Error.ExpectedVarDeclOrFn { .token=token_index };In addition it would allow T to be implicitly casted to the tag type. See #959
Here's an example use case:
In this example
SingleTokenErrormakes a type and returns it. So here we have a bunch of tagged union fields with a bunch of matching field names and types. Importantly for this proposal, the types can be found by doing field access on the tagged union:Error.InvalidTokenresults in both the union tag type and the payload type.This proposal is to allow, for example, implicitly casting
InvalidTokentoError. You could change this code:to this:
In addition it would allow T to be implicitly casted to the tag type. See #959