On Monday, 4 November 2019 at 00:16:53 UTC, Superstar64 wrote:
Consider the following Java code.
--
import java.util.function.Function;
public class Main{

    //basic Rank2 type
    static interface Stringer{
        <A> String show(Function<A,String> type, A that);
    }

    static class Say implements Stringer {
        public <A> String show(Function<A,String> type, A that){
            return type.apply(that);
        }
    }

    static class Shout implements Stringer {
        public <A> String show(Function<A,String> type, A that){
            return type.apply(that) + "!!!";
        }
    }

}
--
This uses Java's generics' type erasure to create a basic rank 2 type. What are some clean ways to implement something similar in D, in a type safe manner if preferable?

String show(A)(Stringer stringer, Function<A,String> type, A that) {
       Shout shout = cast(Shout)stringer;
       if(shout !is null) {
             shout.show(type, that); return;
       }

       Say say = cast(Say)stringer;
       if(say !is null) {
             say.show(type, that); return;
       }

      assert(false, "Unsupported");
}

Stringer stringer = new Shout();
stringer.show(type, that);

Reply via email to