简体   繁体   中英

How to create ScalaZ Disjunction from Java?

Why? I am writing tests for Java-Scala Adaptor class.

How to create left and right disjunction for \\/[String, Int] in Java?

Symbols in Scala method names actually have desugaring, used to deterministically represent them in JVM: \\ turns to $bslash , / to $div and - to $minus . Also, Java cannot understand Scala's Nothing type, so we need to insert an actual type of the missing argument. To ease the pain of using Disjunction from Java, we can construct a following little helper (along with test method):

import scalaz.*;

public class JavaDisjunctionHelper {

    public static <A, B> $bslash$div<A, B> left(A a) {
        return new $minus$bslash$div(a);
    }

    public static <A, B> $bslash$div<A, B> right(B b) {
        return new $bslash$div$minus(b);
    }

    public static void main(String[] args) {
        $bslash$div<String, Integer> p = left("z");
        $bslash$div<String, Integer> q = right(3);
        System.out.println(p); // -\/("z")
        System.out.println(q); // \/-(3)
    }
}

That seems to work on Java 8 and probably any version starting from Java 5, assuming that you have a compatible version of Scala runtime and Scalaz.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM