Created
June 1, 2013 05:14
-
-
Save kagamilove0707/5689348 to your computer and use it in GitHub Desktop.
Javaで幽霊型しようとした記録ですー>ω<
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
package com.github.kagamilove0707.safelist; | |
public interface Callback<A> { | |
public void call(A val); | |
} |
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
package com.github.kagamilove0707.safelist; | |
import static com.github.kagamilove0707.safelist.SafeList.*; | |
public class Main { | |
public static void main(String[] args) { | |
SafeList<String, ?> list = cons(">ω<", SafeList.<String>nil()); | |
maybe(list, new Callback<SafeList<String, NonEmpty>>() { | |
@Override | |
public void call(SafeList<String, NonEmpty> list) { | |
System.out.println(head(list)); | |
} | |
}); | |
} | |
} |
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
package com.github.kagamilove0707.safelist; | |
public abstract class SafeList<A, B extends SafeListState> { | |
Class<B> type; | |
protected SafeList(@SuppressWarnings("unchecked") B ..._type) { | |
@SuppressWarnings("unchecked") | |
Class<B> type = (Class<B>) _type.getClass().getComponentType(); | |
this.type = type; | |
} | |
public static <A> A head(SafeList<A, NonEmpty> list) { | |
return list.getHead(); | |
} | |
public static <A> SafeList<A, ?> tail(SafeList<A, NonEmpty> list){ | |
return list.getTail(); | |
} | |
public static <A> SafeList<A, NonEmpty> cons(final A head, final SafeList<A, ?> tail) { | |
return new SafeList<A, NonEmpty>(){ | |
@Override | |
protected A getHead() { | |
return head; | |
} | |
@Override | |
protected SafeList<A, ?> getTail() { | |
return tail; | |
} | |
}; | |
} | |
public static <A> SafeList<A, Empty> nil() { | |
return new SafeList<A, Empty>(){}; | |
} | |
@SuppressWarnings("unchecked") | |
public static <A> void maybe(SafeList<A, ?> list, Callback<SafeList<A, NonEmpty>> callback) { | |
if(list.type.equals(NonEmpty.class)) { | |
callback.call((SafeList<A, NonEmpty>)list); | |
} | |
} | |
protected A getHead(){ | |
return null; | |
} | |
protected SafeList<A, ?> getTail(){ | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment