What Is Type Safety?
Type-safe code refers to memory locations that are authorized to be accessed. For example, type-safe code cannot read values from private fields of other objects. It can only be read from well-defined allowed access types.
Type safety
- Type-safe code refers to access that is authorized to access
- The language of toys for academic research often raises requirements for type safety. On the other hand, many languages produce type safety manually, which often proves to require thousands of checks. However, some languages, such as standard ML, have strict definitions of semantics, and Java also provides type safety [source request]. Other languages such as Haskell are also considered type safe. Ignoring the nature of the language definition for the time being, some errors that occur during execution should be attributed to defects in the implementation, or libraries written in other languages; such errors may cause The type of the case is no longer safe.
- Memory management in type-safe languages
- To achieve a complete type-safe language, it requires at least garbage collection or increased memory allocation and deallocation restrictions (this section focuses on the former). More specifically, hanging indicators are not allowed to exist across different structure types. There is a technical reason for this: a typed language is assumed (eg Pascal requires that memory allocated must be explicitly released). If there is a hanging indicator that still points to the previous memory address, the new data structure may be allocated to the same space. For example, if you initialize an indicator that points to the data structure of the integer area, but the indicator area of the new object is allocated in the place of the integer, then the indicator area can be simply changed to anything by changing the value of the integer area (the indicator is suspended by indirect reference) ). The language is not type safe because what has happened has not been specified when the indicator changes. Most type-safe languages satisfy memory management using garbage collection.
- In languages that allow index arithmetic, it is best to implement the garbage collector, so in a type-unsafe language or a language where type-safety may fail, it is best to implement such a library. C and C ++ are often used.
- Type safety and strong typing
- In the definition of various strong types, they often become synonyms for type safety; however, type safety and dynamic typing are not mutually exclusive. Dynamic typing can also be regarded as a very loose statically typed language, and all programs with correct syntax are well-typed; as long as its dynamic semantics can guarantee that there will never be a program "wrong", it can meet the above definition, And it can be called type safety.