定義:
型別安全(type safety)可以靜態方式實施,及早在編譯時期就捕捉到潛藏的錯誤;或者以動態方式,在執行時期關聯型別的資訊,並在必要時檢測即將發生的錯誤。型別安全是程式語言的性質,而不是程式所自有的。例如,有可能以型別不安全的語言,編寫出型別安全的程式。在此是以程式語言為主,而不討論以個人能力維護的型別安全。(p.s. 以上內容來自Wiki)
Note:
針對Swift語言:
Swift is a type-safe language, which means the language helps you to be clear about the types of values your code can work with. If part of your code requires a String
, type safety prevents you from passing it an Int
by mistake. Likewise, type safety prevents you from accidentally passing an optional String
to a piece of code that requires a non-optional String
. Type safety helps you catch and fix errors as early as possible in the development process.
優缺點:
優點: 能讓開發者在開發時提早發現錯誤 (p.s. 在靜態語言狀況下)
Ref:
https://stackoverflow.com/questions/260626/what-is-type-safe
https://zh.wikipedia.org/wiki/%E5%9E%8B%E5%88%A5%E5%AE%89%E5%85%A8
https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html