What is the difference between casting and converting




















Conversion will entail some sort of parsing, or deeper analysis and conversion of the source data. Casting does not parse. It simply attempts a match at some polymorphic level. Casting is the creation of a value of one type from another value of another type. Conversion is a type of casting in which the internal representation of the value must also be changed rather than just its interpretation.

The distinction is important and the point is made in the comment because only conversions may be created by a conversion-operator-declarator. Therefore, only implicit or explicit conversions may be created in code. A non-conversion implicit cast is always available for subtype-to-supertype casts, and a non-conversion explicit cast is always available for supertype-to-subtype casts.

No other non-conversion casts are allowed. In this context, casting means that you are exposing an object of a given type for manipulation as some other type, conversion means that you are actually changing an object of a given type to an object of another type. This page of the MSDN C documentation suggests that a cast is specific instance of conversion: the "explicit conversion. Therefor the compiler requires you to explicitly state that you are aware of this and want to do it anyway, through use of the explicit cast syntax:.

Conversion takes two types that are not necessarily related in any way, and attempts to convert one into the other through some process, such as parsing. If all known conversion algorithms fail, the process may either throw an exception or return a default value:. Eric's references to syntactic conversion vs. A cast is syntactical, and may or may not involve a conversion depending on the type of cast.

Eric C is saying that casting to a different type always involves a conversion, though that conversion may not even change the internal representation of the instance.

Casting and Conversion are basically the same concept in C , except that a conversion may be done using any method such as Object.

Casting is only done with the casting operator T E , that is described in other posts, and may make use of conversions or boxing. Which conversion method does it use? The compiler decides based on the classes and libraries provided to the compiler at compile-time. If an implicit conversion exists, you are not required to use the casting operator.

If only explicit conversions exist, you must use the casting operator. You can create explicit and implicit conversion operators in your own classes.

Note: conversions can make the data look very similar or nothing like the original type to you and me, but it's all defined by the conversion methods, and makes it legal to the compiler. But if you access s , in for example a Console. WriteLine , you will receive a runtime InvalidCastException. So, the cast operator still attempts to use conversion at access time, but will settle for boxing during assignment.

And the answers of "No" are overwhelmingly upvoted. A compiler compiles from denotational semantics to operational semantics. The compilation is not bijective, i. Java, LLVM, asm. Scala, Python, C , C via Emscripten, etc. Thus the two layers not the same. Thus most obviously a ' cast ' and a ' conversion ' are not the same thing.

My answer here is pointing out that the terms apply to two different layers of semantics. Casts apply to the semantics of what the denotational layer input syntax of the compiler knows about. Conversions apply to the semantics of what the operational runtime or intermediate bytecode layer knows about. I used the standard term of 'erased' to describe what happens to denotational semantics that aren't explicitly recorded in the operational semantics layer.

For example, reified generics are an example of recording denotational semantics in the operational semantics layer, but they have the disadvantage of making the operational semantics layer incompatible with higher-order denotational semantics, e. Come on guys, stop downvoting someone who knows a lot more than you do. Do your homework first before you vote. A cast may e.

The downvotes on my answer and the upvoting on Marc Gavin's comment indicates to me that most people don't understand the differences between denotational semantics and operational execution semantics. I will state Eric Lippert's answer more simply and more generally for all languages, including C. A cast is syntax so like all syntax is erased at compile-time ; whereas, a conversion causes some action at runtime.

That is a true statement for every computer language that I am aware of in the entire universe. Note that the above statement does not say that casting and conversions are mutually exclusive. A cast may cause a conversion at runtime, but there are cases where it may not. The reason we have two distinct words, i.

It is important that we maintain this separation-of-concepts, because in some programming languages the cast never causes a conversion. Also so that we understand implicit casting e. The reason I wrote this answer is because I want to help readers understand in terms of being multilingual with computer languages.

And also to see how that general definition correctly applies in the C case as well. Also I wanted to help readers see how I generalize concepts in my mind, which helps me as computer language designer.

I am trying to pass along the gift of a very reductionist, abstract way of thinking. But I am also trying to explain this in a very practical way. Please feel free to let me know in the comments if I need to improve the elucidation. The recipe is what is happening in syntax. Syntax is always erased, and replaced with either nothing or some runtime code.

For example, I can write a cast in C that does nothing and is entirely erased at compile-time when it is does not cause a change in the storage requirements or is upcasting. We can clearly see that a cast is just syntax, that makes no change to the runtime code. That can be used for documentation purposes yet noisy , but it is essential in languages that have type inference, where a cast is sometimes necessary to tell the compiler what type you wish it to infer.

For an example , in Scala a None has the type Option[Nothing] where Nothing is the bottom type that is the sub -type of all possible types not super -type. So sometimes when using None, the type needs to be casted to a specific type, because Scala only does local type inference , thus can't always infer the type you intended.

A cast could know at compile-time that it requires a type conversion, e. The cast i. Thus we can clearly see that equating all casts with explicit conversion, is an error of implication in the MSDN documentation. That documentation is intending to say that explicit conversion requires a cast operator , but it should not be trying to also imply that all casts are explicit conversions. I am confident that Eric Lippert can clear this up when he writes the blog he promised in his answer.

ADD : From the comments and chat, I can see that there is some confusion about the meaning of the term erased. The term 'erased' is used to describe information that was known at compile-time, which is not known at runtime. For example, types can be erased in non-reified generics, and it is called type erasure. Generally speaking all the syntax is erased, because generally CLI is not bijective invertible, and one-to-one with C. You cannot always go backwards from some arbitrary CLI code back to the exact C source code.

This means information has been erased. Those who say erased is not the right term, are conflating the implementation of a cast with the semantic of the cast. The cast is a higher-level semantic I think it is actually higher than syntax , it is denotational semantics at least in case of upcasting and downcasting that says at that level of semantics that we want to cast the type.

Now how that gets done at runtime is entirely different level of semantics. In some languages it might always be a NOOP. Others are implicit, and are applied in certain cases when an expression of one type is used in a context that needs an expression of a different type.

The conversion performed is exactly the same in either case. While casting, you read the instance of one class as if it is the instance of another class. It could be appliccable for this pair of classes or not. No runtime work except checking. Often the possible incompatibility could be caught on compiling stage. While converting, you recombine or recount fields of an instance of one class into an instance of another class.

If there is a function for it, it could be applicable or not for this very instance. All the work is done during runtime. No error could be checked while compiling.

Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Is there a difference between casting and converting types in imperative programming languages? Ask Question. Asked 9 years, 9 months ago. Active 6 years, 6 months ago.

Viewed 7k times. Improve this question. Community Bot 1. Add a comment. Active Oldest Votes. Theoretically speaking , the two concepts are vastly different: Type Casting refers to exchanging one type roughly speaking, an object structure for another while Type Conversion refers to translating of values roughly speaking, contents of the object , so that they may be interpreted as belonging to a new type.

Therefor the compiler requires you to explicitly state that you are aware of this and want to do it anyway, through use of the explicit cast syntax:. Conversion takes two types that are not necessarily related in any way, and attempts to convert one into the other through some process, such as parsing.

If all known conversion algorithms fail, the process may either throw an exception or return a default value:. Eric's references to syntactic conversion vs. A cast is syntactical, and may or may not involve a conversion depending on the type of cast. Eric C is saying that casting to a different type always involves a conversion, though that conversion may not even change the internal representation of the instance. Casting and Conversion are basically the same concept in C , except that a conversion may be done using any method such as Object.

Casting is only done with the casting operator T E , that is described in other posts, and may make use of conversions or boxing. Which conversion method does it use? The compiler decides based on the classes and libraries provided to the compiler at compile-time. If an implicit conversion exists, you are not required to use the casting operator. If only explicit conversions exist, you must use the casting operator. You can create explicit and implicit conversion operators in your own classes.

Note: conversions can make the data look very similar or nothing like the original type to you and me, but it's all defined by the conversion methods, and makes it legal to the compiler. But if you access s , in for example a Console. WriteLine , you will receive a runtime InvalidCastException.

So, the cast operator still attempts to use conversion at access time, but will settle for boxing during assignment. And the answers of "No" are overwhelmingly upvoted. A compiler compiles from denotational semantics to operational semantics. The compilation is not bijective, i. Java, LLVM, asm. Scala, Python, C , C via Emscripten, etc. Thus the two layers not the same. Thus most obviously a ' cast ' and a ' conversion ' are not the same thing.

My answer here is pointing out that the terms apply to two different layers of semantics. Casts apply to the semantics of what the denotational layer input syntax of the compiler knows about. Conversions apply to the semantics of what the operational runtime or intermediate bytecode layer knows about. I used the standard term of 'erased' to describe what happens to denotational semantics that aren't explicitly recorded in the operational semantics layer. For example, reified generics are an example of recording denotational semantics in the operational semantics layer, but they have the disadvantage of making the operational semantics layer incompatible with higher-order denotational semantics, e.

Come on guys, stop downvoting someone who knows a lot more than you do. Do your homework first before you vote. A cast may e. The downvotes on my answer and the upvoting on Marc Gavin's comment indicates to me that most people don't understand the differences between denotational semantics and operational execution semantics. I will state Eric Lippert's answer more simply and more generally for all languages, including C.

A cast is syntax so like all syntax is erased at compile-time ; whereas, a conversion causes some action at runtime. That is a true statement for every computer language that I am aware of in the entire universe. Note that the above statement does not say that casting and conversions are mutually exclusive.

A cast may cause a conversion at runtime, but there are cases where it may not. The reason we have two distinct words, i. It is important that we maintain this separation-of-concepts, because in some programming languages the cast never causes a conversion. Also so that we understand implicit casting e. The reason I wrote this answer is because I want to help readers understand in terms of being multilingual with computer languages.

And also to see how that general definition correctly applies in the C case as well. Also I wanted to help readers see how I generalize concepts in my mind, which helps me as computer language designer. I am trying to pass along the gift of a very reductionist, abstract way of thinking.

But I am also trying to explain this in a very practical way. Please feel free to let me know in the comments if I need to improve the elucidation.



0コメント

  • 1000 / 1000