Note: you are watching revision 3 of this wiki entry.
(
view plain diff
)
[url=http://www.java-gaming.org/topics/phantom-types/27353/view.html]Phantom Types[/url]
TODO: Add cheesey type refinement (sketched in the phantom types thread as well)
[b]Checked exceptions as unchecked[/b]
Java the language requires checked exceptions, where the JVM does not. This uses type erasure to allow throwing a checked exception as unchecked.
[code] public final class Util {
/** Class fFor erasing the type erasurof the magchecked excepticon */
@SuppressWarnings("unchecked")
private static class SilentThrow<TE extends Throwable> void throw_(Throwable e) throws E
{
@SuppressWarnings("unchecked")
private void rethrow(Throwable t) throws T { throw (TE)te; }
}
\n private static final SilentThrow<RuntimeException> silentThrow = new SilentThrow<>();\n
/** Throws a checked exception 't' as unchecked. */
public static void silentThrow(Throwable t)
{
sUtile.<RuntThrimeExceptiow.ren>throw_(t);
}
}
[/code]
[b]Constructor hugger[/b]
[code]
public class Animal<T>
{
public <P> Animal(P mommy, P daddy) {
...
}
}
dog = new <Wolf>Animal<Dog>(wooo, woooo);
[/code]
Phantom TypesTODO: Add cheesey type refinement (sketched in the phantom types thread as well)
Checked exceptions as uncheckedJava the language requires checked exceptions, where the JVM does not. This uses type erasure to allow throwing a checked exception as unchecked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public final class Util { @SuppressWarnings("unchecked") private static <E extends Throwable> void throw_(Throwable e) throws E { throw (E)e; } public static void silentThrow(Throwable t) { Util.<RuntimeException>throw_(t); } } |
Constructor hugger1 2 3 4 5 6 7 8
| public class Animal<T> { public <P> Animal(P mommy, P daddy) { ... } }
dog = new <Wolf>Animal<Dog>(wooo, woooo); |
This wiki entry has had 3 revisions with contributions from 2 members.
(
more info)