[External] : Re: Final variable initialization problem with exhaustive switch

John Rose john.r.rose at oracle.com
Wed Dec 1 04:22:17 UTC 2021



> On Nov 30, 2021, at 8:06 AM, Remi Forax <forax at univ-mlv.fr> wrote:
> 
> Fortunately, a warning does not break the code.

That would help. Such a warning would note where there are two good answers and the language has to pick one but the user might well be thinking of the other. 

There should be ways to fix the warning very simply and without excess noise. For the legacy behavior I think this line works:

  default -> break;

There is no correspondingly simple way to ask for an exhaustiveness check but some have suggested in the past this new syntax:

   default -> throw;

The meaning of a lone throw would be “make a suitable exception for this spot in thhis switch and throw it”. Defining suitable is tricky since it is a DWIM request. 

Others have suggested putting the explicit selection at the top, by allowing the switch to accept an extra keyword to say whether it is supposed to be exhaustive. 

The problem with any of these fixes is they feel like a complexity tax on the language for very little expressive gain. 


More information about the amber-dev mailing list