简体   繁体   English

如何从Java创建ScalaZ Disjunction?

[英]How to create ScalaZ Disjunction from Java?

Why? 为什么? I am writing tests for Java-Scala Adaptor class. 我正在为Java-Scala Adapter类编写测试。

How to create left and right disjunction for \\/[String, Int] in Java? 如何在Java中为\\/[String, Int]创建左右分离?

Symbols in Scala method names actually have desugaring, used to deterministically represent them in JVM: \\ turns to $bslash , / to $div and - to $minus . Scala方法名称中的符号实际上具有desugaring,用于在JVM中确定性地表示它们: \\转向$bslash/$div-$minus Also, Java cannot understand Scala's Nothing type, so we need to insert an actual type of the missing argument. 此外,Java无法理解Scala的Nothing类型,因此我们需要插入缺少参数的实际类型。 To ease the pain of using Disjunction from Java, we can construct a following little helper (along with test method): 为了减轻使用Disjunction from Java的痛苦,我们可以构造一个以下的小助手(以及测试方法):

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. 这似乎适用于Java 8,可能适用于从Java 5开始的任何版本,假设您有Scala运行时和Scalaz的兼容版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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