What is The Type Conversion?




What is The Type Conversion?

During type checking, a mismatch occurs between the actual type of an argument and the expected type for that operation, then either,

  • The type mismatch may be flagged as an error and an appropriate error action taken, or.

  • A coercion i.e. type conversion may be applied to change the type of the actual argument to the correct type.

A type conversion is an operation with the signature.

conversion_op : type, -- + type 2 

That is, the conversion takes a data object of one type and produces the corresponding data object of a different type. Most languages provide type conversions in two ways −

  • As a set of built-in function that the programmer may explicitly invoke to effect the conversion.

  • As coercions invoked automatically in certain cases of type mismatch.

Most of the time, operators and functions automatically convert the values given to them to the right type. This is called “type conversion”.

The compiler provides implicit type conversions when operands have mismatched types, or when you call a function using an argument whose type does not match the function’s corresponding parameter.

Programs also perform implicit type conversion as necessary when initializing variables or otherwise assigning values to them. If the necessary conversion is not possible, the compiler issues an error message.