@@ -1061,46 +1061,31 @@ ghci> getMaybeString Nothing = "Nothing!"
1061
1061
dangerous = \ (Just x) -> x
1062
1062
```
1063
1063
1064
- みたいに書きたくなったら、 ↓のような` case ` に変換するしかない( ` LambdaCase ` についてはいつか脚注に)。
1064
+ もし上↑のように書きたくなったら、必ず下 ↓のような` case ` 式に変換してください [ ^ 3 ] :
1065
1065
1066
- ```
1066
+ [ ^ 3 ] : ` LambdaCase ` というGHCの言語拡張を使う手もありますので、興味のある方は調べてみてください。
1067
+
1068
+ ``` haskell
1067
1069
dangerous = \ mx ->
1068
1070
case mx of
1069
1071
Just x -> ...
1070
1072
Nothing -> ...
1071
1073
```
1072
1074
1073
- 結果、うっかり` Nothing ` が渡されてエラーになってしまうことも
1074
-
1075
- ```
1076
- ghci> dangerous Nothing
1077
- *** Exception: <interactive>:8:2-15: Non-exhaustive patterns in lambda
1078
- ```
1075
+ ` dangerous = \(Just x) -> x ` のように分岐できないパターンマッチをするのは、ここまで説明したとおり例外が発生する原因になるので、` -Wall ` というオプション[ ^ 4 ] で警告を有効にしていると警告が出ます:
1079
1076
1080
- 警告を有効にしても教えてくれない😱
1077
+ [ ^ 4 ] : GHC 9.0以前の場合、 ` -Wall ` を有効にしてもこの警告は出ません。 ` -Wincomplete-uni-patterns ` を有効にしなければならないのでご注意ください。
1081
1078
1082
- ```
1079
+ ``` haskell
1080
+ -- GHCiで警告を有効にする手段については課題hoge(7?)を参照してください
1083
1081
ghci> : set - Wall
1084
- ghci> dangerous = (\(Just x) -> x)
1085
- ```
1086
-
1087
- ` -Wincomplete-uni-patterns ` を有効にしないといけない!
1088
1082
1089
- ```
1090
- ghci> :set -Wincomplete-uni-patterns
1091
1083
ghci> dangerous = \ (Just x) -> x
1092
1084
1093
- <interactive>:10:2 : warning: [-Wincomplete-uni-patterns]
1085
+ < interactive>: 11 : 13 : warning: [- Wincomplete - uni- patterns]
1094
1086
Pattern match(es) are non- exhaustive
1095
- In a lambda abstraction: Patterns not matched: Nothing
1087
+ In a lambda abstraction:
1088
+ Patterns of type ‘ Maybe a’ not matched: Nothing
1096
1089
```
1097
1090
1098
- [ GHCの将来のバージョンで、` -Wincomplete-uni-patterns ` が` -Wall ` に含まれる] [ 7 ] かも。
1099
-
1100
- [ 7 ] : https://gitlab.haskell.org/ghc/ghc/issues/15656
1101
-
1102
- ` -Wall ` を有効にしても有効にならない警告の一覧は[ GHC Users Guide] [ 8 ] を参照。
1103
-
1104
- < https://functor.tokyo/blog/2017-07-28-ghc-warnings-you-should-enable >
1105
-
1106
- [ 8 ] : https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/using-warnings.html#ghc-flag--Wall
1091
+ ` -Wall ` というオプションは、GHCが出すことのできるほとんどの警告を有効にするオプションです。普段プログラムを書くときはこのオプションを有効にして、問題のあるコードを見つける手がかりにするのをおすすめします。
0 commit comments