Skip to content

Instantly share code, notes, and snippets.

@ChrLipp
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrLipp/10017503 to your computer and use it in GitHub Desktop.
Save ChrLipp/10017503 to your computer and use it in GitHub Desktop.
// 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