Created
April 20, 2017 19:12
-
-
Save jbgi/d6b677d084fafc641fe01f7ffd00591c to your computer and use it in GitHub Desktop.
Revisions
-
jbgi created this gist
Apr 20, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ import java.io.IOException; import org.derive4j.hkt.__; public abstract class Label<T> { private Label(){} public abstract T apply(String s); public abstract String unwrap(T lbl); public abstract <f> __<f, String> subst(__<f, T> fa); static Label<?> Label = new Label<String>() { @Override public String apply(String s) { return s; } @Override public String unwrap(String lbl) { return lbl; } @Override public <f> __<f, String> subst(__<f, String> fa) { return fa; } }; public static void main(String[] args) throws IOException { program(Label).run(); } // "Slight" inconvenience vs scala: you have to write your program inside method(s) parametrized by the Label(s): static <T> IO<Unit> program(Label<T> Label) { class Toto { private final T wrapped; Toto(T wrapped){this.wrapped = wrapped;} } Toto toto = new Toto(Label.apply("Hello World")); String unwrapped = Label.unwrap(toto.wrapped); return IO.effect(() -> System.out.println(unwrapped)); } }