Panic
The panic
keyword is similar to the todo
keyword,
but it is used to crash the program when the program has reached a point that
should never be reached.
panic
キーワードは todo
キーワードと似ていますが、プログラムが決して到達すべきでないポイントに到達したときに、プログラムをクラッシュさせるために使用できます。
This keyword should almost never be used! It may be useful in initial prototypes and scripts, but its use in a library or production application is a sign that the design could be improved. With well designed types the type system can typically be used to make these invalid states unrepresentable.
このキーワードはほとんどの場合、使用されるべきではありません!初期のプロトタイプやスクリプトでは有用かもしれませんが、ライブラリや本番のアプリケーションでの仕様は、設計を改善できる可能性があることを示すサインとなります。うまく設計された型であれば、型システムを使用してこれらの無効な状態を表現できないようにすることができます。
import gleam/io
pub fn main() {
print_score(10)
print_score(100_000)
print_score(-1)
}
pub fn print_score(score: Int) {
case score {
score if score > 1000 -> io.println("High score!")
score if score > 0 -> io.println("Still working on it")
_ -> panic as "Scores should never be negative!"
}
}