Do “type-safe” and “strongly typed” mean the same

2020-05-24 20:15发布

Do "type-safe" and "strongly typed" mean the same thing?

标签: c# types
5条回答
闹够了就滚
2楼-- · 2020-05-24 20:34

No, not necessarily - although it depends on your definition of the terms, and there are no very clear and widely accepted definitions.

For instance, dynamic programming languages are often type safe, but not strongly typed. In other words, there's no compile-time type information determining what you can and can't do with a type, but at execution time the runtime makes sure you don't use one type as if it were another.

For example, in C# 4.0, you can do:

dynamic foo = "hello";
dynamic length = foo.Length; // Uses String.Length at execution time
foo = new int[] { 10, 20, 30 };
length = foo.Length; // Uses Array.Length at execution time
dynamic bar = (FileStream) foo; // Fails!

The last line is the key to it being type-safe: there's no safe conversion from an int array to a FileStream, so the operation fails - instead of treating the bytes of the array object as if they were a FileStream.

EDIT: C# is normally both "strongly typed" (as a language) and type safe: the compiler won't let you attempt to make arbitrary calls on an object, and the runtime won't let you perform inappropriate conversions.

I'm not entirely sure where unsafe code fits in - I don't know enough about it to comment, I'm afraid.

Dynamic typing in C# 4 allows weakly typed but still type-safe code, as shown above.

Note that foreach performs an implicit conversion, making it a sort of hybrid:

ArrayList list = new ArrayList();
list.Add("foo");

foreach (FileStream stream in list)
{
    ...
}

This will compile (there was another question on this recently) but will fail at execution time. Ironically, that's because you're trying to be strongly typed with respect to the stream variable, which means you have to perform a cast on the result of the iterator.

查看更多
祖国的老花朵
3楼-- · 2020-05-24 20:46

"Type Safe" means that there's no casting involved and no run-time type errors can occur.

Some people argue that "Strongly Typed" mean nothing, or "it's good", or "i'm comfortable with it".

Anyway, "Type Safe" relates to a portion of code or API, when "Strongly Typed" refers to a whole language or platform.

查看更多
劫难
4楼-- · 2020-05-24 20:47

ype safe means preventing programs from accessing memory outside the bounds of an object's public properties. When code is not type-safe, unwanted side effects can occur. Type-safety is important for assembly isolation and security enforcement. When code is type- safe, the common language runtime can completely isolate assemblies from each other

查看更多
放荡不羁爱自由
5楼-- · 2020-05-24 20:49

They are the basically same, it's just a matter of interpretation:

From wikipedia:

Type safety:

Type safety is synonymous with one of the many definitions of strong typing; but type safety and dynamic typing are mutually compatible. A dynamically typed language such as Smalltalk can be seen as a strongly typed language with a very permissive type system where any syntactically correct program is well-typed; as long as its dynamic semantics ensures that no such program ever "goes wrong" in an appropriate sense, it satisfies the definition above and can be called type-safe.

查看更多
▲ chillily
6楼-- · 2020-05-24 21:00

Good question. Read this wikipedia entry, here's an extract:

Benjamin C. Pierce, author of Types and Programming Languages and Advanced Types and Programming Languages, says, "I spent a few weeks... trying to sort out the terminology of "strongly typed," "statically typed," "safe," etc., and found it amazingly difficult.... The usage of these terms is so various as to render them almost useless."

查看更多
登录 后发表回答