Module is used to extend a class functionality. This is usually used for the common functionalities cross over a different kind of classes.
The module definition is basically below.
module M {
/* ... Defines a public method
to extend a host class which this is mixined into. */
}
The module is mixed into a class by mixin
keyword.
class A {
mixin M;
/* ... */
}
mixin
can have multiple modules like this.
class A {
mixin M1, M2, M3;
/* ... */
}
By the module mixed into a class, the methods defined in the module are added to the class.
For example, when the module M
having the method of method1
is mixed into a class of A
, A
could have and use the method of method1
. Note that the method of method1
must be a public method then.
module M {
public method1() {
System.println("This is a method1");
}
}
class A {
mixin M;
}
new A().method1(); // => This is a method1
module M {
public method1() {
System.println("This is a method1");
}
}
class A {
mixin M;
}
new A().method1(); // => This is a method1
This is a method1
module Printable {
public print() {
System.println("value = " + @value);
}
}
class A {
mixin Printable;
@value = "'This is A.'";
}
class B {
mixin Printable;
@value = 256;
}
var a = new A();
var b = new B();
a.print();
b.print();
value = 'This is A.'
value = 256
module M1 {
public method1() {
System.println("This is a method1");
}
}
module M2 {
public method2() {
System.println("This is a method2");
}
}
class A {
mixin M1, M2;
}
var a = new A();
a.method1(); // => This is a method1
a.method2(); // => This is a method2
This is a method1
This is a method2
module M0 {
public method0() {
System.println("This is a method0");
}
}
module M1 {
public method1() {
System.println("This is a method1");
}
}
module M2 {
public method2() {
System.println("This is a method2");
}
}
module M3 {
public method3() {
System.println("This is a method3");
}
}
class A {
mixin M0;
mixin M1, M2, M3;
}
var a = new A();
a.method0(); // => This is a method0
a.method1(); // => This is a method1
a.method2(); // => This is a method2
a.method3(); // => This is a method3
This is a method0
This is a method1
This is a method2
This is a method3