简体   繁体   English

Scala / Java语法,返回接口实现

[英]Scala/Java syntax, return interface implementation

What does this line of code mean in the code at the bottom? 此代码行在底部的代码中意味着什么?

return Promise.<SimpleResult>pure  //why is the dot

I am new to Scala and Java 6, what is the related topic/terminology? 我是Scala和Java 6的新手,相关的主题/术语是什么?

If the framework doesn't find an action method for a request, the onHandlerNotFound operation will be called: 如果框架找不到请求的操作方法,则将调用onHandlerNotFound操作:

import play.*;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;

import static play.mvc.Results.*;

public class Global extends GlobalSettings {

    public Promise<SimpleResult> onHandlerNotFound(RequestHeader request) {
        return Promise.<SimpleResult>pure(notFound(
            views.html.notFoundPage.render(request.uri())
        ));
    }

} }

Update : 更新

play.libs.F.Java file play.libs.F.Java文件

public static class Promise<A> {

    public static <A> Promise<A> pure(final A a) {
        return FPromiseHelper.pure(a);
    }
}

What is <A> and Promise<A> ? 什么是<A>Promise<A>

Promise.pure() is a generic method, parameterized with some type T. Promise.pure()是通用方法,使用某些T类型进行参数化。

Promise.<SimpleResult>pure() calls this method with SimpleResult as the generic type. Promise.<SimpleResult>pure()以SimpleResult作为通用类型调用此方法。 Most of the time, doing it is optional because the compiler infers the generic type from the arguments of the method or from the type of the variable the result is assigned to, like in 大多数情况下,这样做是可选的,因为编译器会根据方法的参数或结果分配给变量的类型来推断泛型,例如

List<String> s = Collections.emptyList();

which is a shortcut for 这是

List<String> s = Collections.<String>emptyList();

or 要么

Set<String> Collections.singleton("hello");

which is a shortcut for 这是

Set<String> Collections.<String>singleton("hello");

Sometimes type inference system can't fully serve its purpose and fails to automatically deduce the return type of the call of the generic method. 有时类型推断系统不能完全满足其目的,并且无法自动推断出泛型方法的调用的返回类型。 To specify such a type, this construct exists - you help to infer the correct type of the expression. 为了指定这种类型,此结构存在-您可以帮助推断表达式的正确类型。

Look at JLS for more. 有关更多信息,请查看JLS ( Formal definition ) 正式定义

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

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