Skip to content

Latest commit

 

History

History
60 lines (46 loc) · 895 Bytes

mixin.md

File metadata and controls

60 lines (46 loc) · 895 Bytes

Mixin statement

Overview

mixin keyword is used in a Class definition to include Module. As the example below, class Value will become having methods of print and println by mixin.

module Printable {
    public print() {
        System.print(@value);
    }
    public println() {
        System.println(@value);
    }
}

class Value(v) {
    mixin Printable;
    private initialize() {
        @value = v;
    }
}

Examples

Example 1. Normal case

Here is the whole code of the use case of the above example.

Code

module Printable {
    public print() {
        System.print(@value);
    }
    public println() {
        System.println(@value);
    }
}

class Value(v) {
    mixin Printable;
    private initialize() {
        @value = v;
    }
}

var val = new Value(100);
val.println();  // => 100

Result

100