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

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.

主站蜘蛛池模板: 象州县| 兴化市| 海口市| 万源市| 永丰县| 疏勒县| 揭阳市| 丹阳市| 吉木萨尔县| 常山县| 花莲市| 衡南县| 石渠县| 沂水县| 井陉县| 苍梧县| 安国市| 日喀则市| 岳阳市| 芦山县| 哈巴河县| 河曲县| 罗田县| 墨竹工卡县| 张家港市| 高唐县| 晋宁县| 梁山县| 大关县| 镇巴县| 平山县| 贡山| 涡阳县| 霍山县| 东乌珠穆沁旗| 资阳市| 梁平县| 习水县| 普陀区| 泗水县| 辽宁省|