Point to Remember
Coercion Operator is used for casting
Custom Coercion rules can be applied to a class by defining asType method in a class.
Syntax of coercion operator is as
What is Coercion Operator
Coercion operator is used to do type casting of objects in groovy. It converts objects of one type to another type.
Lets take a look at the following examples
int a = 123
String s = (String) a
The the above example, the assignment of a to s will give a ClassCastException in java but when you run the same in groovy it will not give an error since Groovy as defined how to typecast integer to string.
Coercion version of the above example is as follows
int a = 123
String s = a as String
How to Define Custom Coercion Rules.
Let's take an example where we want to convert an object of class A to class B.
class A {
String name
}
class B {
String name...