The handling has conveniently improved with the new switch expressions: Let’s assume we did not specify WEDNESDAY in the above example. This would only have been pointed out later when accessing the variable used in the example that it is not initialized in every case. The compiler wouldn't have recognized this in switch itself. In the old version of switch it was possible to omit the default or to specify a case for individual values, for example WEDNESDAY. Furthermore, this is no longer allowed in the new syntax after the arrow. And even better, of course, still without the need for a break. Instead of just stating a value after the arrow, it is also possible to specify expressions such as assignments or method calls without any problems. Besides, the switch can now return a value, which avoids the need to define auxiliary variables. Conveniently, there is no more need for break: The statements after the arrow are only executed specifically for the case and no fall-through exists with this syntax. With the new "Switch Expressions", expressing case distinctions is made much easier and provides an intuitive notation:įrom this example, we notice some syntactic innovations: In addition to the obvious arrow instead of the colon, multiple values can now be specified after the case. Otherwise, the compiler complains that the variable numOfLetters may not be initialized – unless you have already assigned a value to it initially. In particular, despite the actually complete coverage of the enum values, the default is necessary. Moreover, we need to set the (artificial) auxiliary variable numOfLetters correctly in each branch. Even worse, a break is needed so that the processing runs without surprise and there is no fall-through. The multiple specifications of values need accustoming, too. First of all, the shown construct does not appear elegant and is also quite long. Let's have a critical look at the source code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |