Last active
August 29, 2015 13:58
-
-
Save ChrLipp/10017503 to your computer and use it in GitHub Desktop.
Groovy traits implementation of http://stackoverflow.com/questions/16115757/groovy-equivalent-to-scala-trait-stackable-modifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tried to provide a Groovy translation of this Scala code: | |
// See http://stackoverflow.com/questions/16115757/groovy-equivalent-to-scala-trait-stackable-modifications | |
import groovy.transform.ForceOverride | |
interface IntQueue { | |
Integer get() | |
void put(Integer x) | |
} | |
trait Incrementing implements IntQueue { | |
@ForceOverride | |
void put(Integer x) {put(x+1) } | |
} | |
trait Filtering implements IntQueue{ | |
@ForceOverride | |
void put(Integer x) { if(x >=0) put(x) } | |
} | |
class BasicIntQueue implements IntQueue{ | |
private buf = new ArrayList<Integer>() | |
Integer get() { buf.remove(0) } | |
void put(Integer x) { buf << x} | |
String toString() { buf.toString() } | |
} | |
def queue = new BasicIntQueue().withTraits Incrementing, Filtering | |
queue.put(-1) | |
queue.put(0) | |
queue.put(1) | |
queue.get() | |
/* | |
java.lang.StackOverflowError | |
at Incrementing$Trait$Helper.put(ConsoleScript26:9) | |
at Incrementing$Trait$Helper$put$1.call(Unknown Source) | |
at Incrementing$TraitAdapter.put(Incrementing$TraitAdapterwrapper) | |
at Incrementing$put.call(Unknown Source) | |
at Incrementing$Trait$Helper.put(ConsoleScript26:9) | |
at Incrementing$Trait$Helper$put$1.call(Unknown Source) | |
at Incrementing$TraitAdapter.put(Incrementing$TraitAdapterwrapper) | |
at Incrementing$put.call(Unknown Source) | |
at Incrementing$Trait$Helper.put(ConsoleScript26:9) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment