Category talk:Wren-trait: Difference between revisions

→‎Source code: Comparable now inherits from Cloneable.
(Added source code for new 'Wren-trait' module.)
 
(→‎Source code: Comparable now inherits from Cloneable.)
Line 1:
===Source code===
<lang ecmascript>/* Module "trait.wren" */
 
/* Cloneable is an abstract class which enables child classes to automatically be
recognized as 'cloneable' by overriding the 'clone' method.
*/
class Cloneable {
clone() { this } /* to be overridden by child class */ }
 
/* CloneableSeq is an abstract class which enables child classes to automatically be
recognized as both Sequences and 'cloneable' by overriding the 'clone' method.
*/
class CloneableSeq is Sequence {
clone() { this } /* to be overridden by child class */ }
 
/*
Comparable is an abstract class which enables child classes to automatically
inherit the comparison operators by just overriding the 'compare' method.
Comparable itself inherits from Cloneable though if one does not wish to override
the 'clone' method, it will just return the current object by default.
*/
class Comparable is Cloneable {
compare(other) {
// This should be overridden in child classes to return -1, 0 or +1
Line 18 ⟶ 34:
==(other) { compare(other) == 0 }
!=(other) { compare(other) != 0 }
 
/* Cloneable is an abstract class which enables child classes to automatically be
recognized as 'cloneable' by overriding the 'clone' method.
*/
class Cloneable {
clone() { /* to be overridden by child class */ }
 
/* CloneableSeq is an abstract class which enables child classes to automatically be
recognized as both Sequences and 'cloneable' by overriding the 'clone' method.
*/
class CloneableSeq is Sequence {
clone() { /* to be overridden by child class */ }
}
 
Line 94 ⟶ 96:
 
// Type aliases for classes in case of any name clashes with other modules.
var Trait_Comparable = Comparable
var Trait_Cloneable = Cloneable
var Trait_CloneableSeq = CloneableSeq
var Trait_Comparable = Comparable
var Trait_Stepped = Stepped
var Trait_Reversed = Reversed</lang>
9,482

edits