官术网_书友最值得收藏!

How it works...

With pattern matching, we saw that patterns are used to test whether a value is of a certain type.

You will also hear some developers say that they test whether the value has a certain shape.

When we find a match we can get to the information specific to that type (or shape). We saw this in the code where we accessed the CourseCodes property, which was specific to the Student type and the  TeachesSubjects property, which was specific to the Professor type.

Lastly, you now need to pay careful attention to the order of your case statements, which now matters. The case statement that uses the when clause is more specific than the statement that simply checks for a Student type. This means that the when case needs to happen before the Student case because both of these cases are of type Student. If the Student case happens before the when clause, it will never trigger the switch for Students that have course code 203.

Another important thing to remember is that the default clause will always be evaluated last, irrespective of where it appears in the switch statement. It is, therefore, good practice to write it as the last clause in a switch statement.

主站蜘蛛池模板: 临城县| 靖西县| 蓬安县| 开原市| 武川县| 安阳县| 祥云县| 上林县| 和政县| 门源| 宣威市| 探索| 中山市| 芷江| 武平县| 长乐市| 舞阳县| 常熟市| 景德镇市| 南江县| 开封县| 涪陵区| 肥西县| 敦化市| 绵竹市| 磐石市| 精河县| 梅河口市| 晴隆县| 扎赉特旗| 龙江县| 诸暨市| 建水县| 阜新市| 分宜县| 兰州市| 福建省| 桐庐县| 洛川县| 大兴区| 山阴县|