jmultimethod

multimethods for Java using annotations
git clone https://logand.com/git/jmultimethod.git/
Log | Files | Refs | LICENSE

commit b0c76f4d1af96ba63c18417310a3d01f1f313d41
parent 000834cada1e227d61f6ec1f96bb75a9499ca802
Author: Tomas Hlavaty <tom@logand.com>
Date:   Sat, 26 Dec 2015 08:27:04 +0100

fix new lines

Diffstat:
Msrc/jmultimethod/DispatchTest.java | 224++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 112 insertions(+), 112 deletions(-)

diff --git a/src/jmultimethod/DispatchTest.java b/src/jmultimethod/DispatchTest.java @@ -1,112 +1,112 @@ -// FLAW: Java reasons about types of variables instead of values - -// for asteroid example see wikipedia visitor pattern - -package jmultimethod; - -public class DispatchTest { - static class Asteroid { - String id; - public Asteroid(String id) {this.id = id;} - public String id() {return id;} - } - static class Spaceship { - String name; - public Spaceship(String name) {this.name = name;} - public String name() {return name;} - } - static class Static { - static void colide(Asteroid x, Asteroid y) { - System.out.println("AA colide " + x.id() + " with " + y.id()); - } - static void colide(Asteroid x, Spaceship y) { - System.out.println("AS colide " + x.id() + " with " + y.name()); - } - static void colide(Spaceship x, Asteroid y) { - System.out.println("SA colide " + x.name() + " with " + y.id()); - } - static void colide(Spaceship x, Spaceship y) { - System.out.println("SS colide " + x.name() + " with " + y.name()); - } - static void colide(Object x, Object y) { - System.out.println("OO colide " + x + " with " + y); - } - static void run() { - run1(); - run2(); - } - static void run1() { - System.out.println("Static: explicitly typed"); - Asteroid a1 = new Asteroid("A1"); - Asteroid a2 = new Asteroid("A2"); - Spaceship s1 = new Spaceship("S1"); - Spaceship s2 = new Spaceship("S2"); - colide(a1, a2); - colide(a1, s1); - colide(s1, a1); - colide(s1, s2); - } - static void run2() { - System.out.println("Static: superclass typed"); - // here is a flaw: the variable type should not be - // specified, it should be infered by the compiler instead - Object a1 = new Asteroid("A1"); - Object a2 = new Asteroid("A2"); - Object s1 = new Spaceship("S1"); - Object s2 = new Spaceship("S2"); - colide(a1, a2); - colide(a1, s1); - colide(s1, a1); - colide(s1, s2); - } - } - static class Dynamic { - void colide(Asteroid x, Asteroid y) { - System.out.println("AA colide " + x.id() + " with " + y.id()); - } - void colide(Asteroid x, Spaceship y) { - System.out.println("AS colide " + x.id() + " with " + y.name()); - } - void colide(Spaceship x, Asteroid y) { - System.out.println("SA colide " + x.name() + " with " + y.id()); - } - void colide(Spaceship x, Spaceship y) { - System.out.println("SS colide " + x.name() + " with " + y.name()); - } - void colide(Object x, Object y) { - System.out.println("OO colide " + x + " with " + y); - } - void run() { - run1(); - run2(); - } - void run1() { - System.out.println("Dynamic: explicitly typed"); - Asteroid a1 = new Asteroid("A1"); - Asteroid a2 = new Asteroid("A2"); - Spaceship s1 = new Spaceship("S1"); - Spaceship s2 = new Spaceship("S2"); - colide(a1, a2); - colide(a1, s1); - colide(s1, a1); - colide(s1, s2); - } - void run2() { - System.out.println("Dynamic: superclass typed"); - // here is the flaw again: dispatch is on variable type - // instead of value type - Object a1 = new Asteroid("A1"); - Object a2 = new Asteroid("A2"); - Object s1 = new Spaceship("S1"); - Object s2 = new Spaceship("S2"); - colide(a1, a2); - colide(a1, s1); - colide(s1, a1); - colide(s1, s2); - } - } - public static void main(String args[]) { - Static.run(); - new Dynamic().run(); - } -} +// FLAW: Java reasons about types of variables instead of values + +// for asteroid example see wikipedia visitor pattern + +package jmultimethod; + +public class DispatchTest { + static class Asteroid { + String id; + public Asteroid(String id) {this.id = id;} + public String id() {return id;} + } + static class Spaceship { + String name; + public Spaceship(String name) {this.name = name;} + public String name() {return name;} + } + static class Static { + static void colide(Asteroid x, Asteroid y) { + System.out.println("AA colide " + x.id() + " with " + y.id()); + } + static void colide(Asteroid x, Spaceship y) { + System.out.println("AS colide " + x.id() + " with " + y.name()); + } + static void colide(Spaceship x, Asteroid y) { + System.out.println("SA colide " + x.name() + " with " + y.id()); + } + static void colide(Spaceship x, Spaceship y) { + System.out.println("SS colide " + x.name() + " with " + y.name()); + } + static void colide(Object x, Object y) { + System.out.println("OO colide " + x + " with " + y); + } + static void run() { + run1(); + run2(); + } + static void run1() { + System.out.println("Static: explicitly typed"); + Asteroid a1 = new Asteroid("A1"); + Asteroid a2 = new Asteroid("A2"); + Spaceship s1 = new Spaceship("S1"); + Spaceship s2 = new Spaceship("S2"); + colide(a1, a2); + colide(a1, s1); + colide(s1, a1); + colide(s1, s2); + } + static void run2() { + System.out.println("Static: superclass typed"); + // here is a flaw: the variable type should not be + // specified, it should be infered by the compiler instead + Object a1 = new Asteroid("A1"); + Object a2 = new Asteroid("A2"); + Object s1 = new Spaceship("S1"); + Object s2 = new Spaceship("S2"); + colide(a1, a2); + colide(a1, s1); + colide(s1, a1); + colide(s1, s2); + } + } + static class Dynamic { + void colide(Asteroid x, Asteroid y) { + System.out.println("AA colide " + x.id() + " with " + y.id()); + } + void colide(Asteroid x, Spaceship y) { + System.out.println("AS colide " + x.id() + " with " + y.name()); + } + void colide(Spaceship x, Asteroid y) { + System.out.println("SA colide " + x.name() + " with " + y.id()); + } + void colide(Spaceship x, Spaceship y) { + System.out.println("SS colide " + x.name() + " with " + y.name()); + } + void colide(Object x, Object y) { + System.out.println("OO colide " + x + " with " + y); + } + void run() { + run1(); + run2(); + } + void run1() { + System.out.println("Dynamic: explicitly typed"); + Asteroid a1 = new Asteroid("A1"); + Asteroid a2 = new Asteroid("A2"); + Spaceship s1 = new Spaceship("S1"); + Spaceship s2 = new Spaceship("S2"); + colide(a1, a2); + colide(a1, s1); + colide(s1, a1); + colide(s1, s2); + } + void run2() { + System.out.println("Dynamic: superclass typed"); + // here is the flaw again: dispatch is on variable type + // instead of value type + Object a1 = new Asteroid("A1"); + Object a2 = new Asteroid("A2"); + Object s1 = new Spaceship("S1"); + Object s2 = new Spaceship("S2"); + colide(a1, a2); + colide(a1, s1); + colide(s1, a1); + colide(s1, s2); + } + } + public static void main(String args[]) { + Static.run(); + new Dynamic().run(); + } +}