Type Coercion
int i = 4;
float f = 3.5;
i = f; // coerce float to int
After the class activity today, you should notice:
- Java forbids lossy implicit type coercions
- Java allows lossy explicit type coercions
- Java forbids type coercions that make no sense, e.g. double to bool
Type Coercion in C
If you did the same chart for C, you would see that every box is a YES. C allows coercions between any primitive types. The only coercion that C forbids is between points and non-pointers.
Type Coercion in Bash
If you did the same chart for Bash, you would see that this is pointless because Bash only has primitive strings, so there is nothing to coerce.
Type Coercion in Haskell/Ada
If you did the same chart for Haskell/Ada, you would see that the entire implicit coercion chart was NOs. Many functional languages do not allow implicit coercions.