In: Computer Science
Explain how coercion can negatively affect type checking but not prevent a language from being strongly typed.
(Programming languages)
Most of the programming languages support conversions in which one data type is converted to Other. These type conversions can be made both implicitly and explicitly which depends on the programmer. Implicit conversion, which is also called coercion, is automatically done by Either Compiler or Interpreter and Some times both in case of programming languages like java.
Explicit conversion, is also known as casting, is performed by Explicit code instructions. Thes codes treat a variable of one data type as belongs to another data type.
The Programming languages that have implicit conversions define that the rules will automatically be implemented when fundamental values are involved.
Let Consider Following Code for better Understanding of implicit and explicit coercion.
double a, b;
a = 3; // implicitly coercion (coercion)
b = (double) 5; // explicitly coercion (casting)
In-Line 2 Integer constant 3 is automatically converted to double before assignment which is Implicit coercion.
In-Line 3 Explicit coercion is performed by involving the destination type with parenthesis.
Every programming language has its rules about how Conversions on Data types can be made Done. Programming Languages with strong typing typically do the little implicit conversion and control the reinterpretation of copies, while Programming languages with weak typing perform many implicit conversions between data types. Weak typing language often allows forcing the compiler to arbitrarily interpret a data item as having different representations. this can be a non-obvious programming error or a technical method to directly deal with the underlying hardware.
In most programming languages, the term coercion is used to indicate an implicit conversion, either through the compilation or during runtime. For example, in an interpretation mixing integer and floating-point numbers (like 10 + 0.20), then either compiler or interpreter or their Combination stuff will automatically convert integer representation into floating-point representation so fractions are not lost.
Explicit type conversions are implied by formulating additional code
(Eg: - adding type identifiers or calling builtin methods) or by coding conversion methods for the compiler to utilize when it otherwise would finish with a type mismatch.
Thus coercion can negatively affect type checking but not prevent a language from being strongly typed as in the case of languages like C, C++