简体   繁体   English

功能接口对象转换

[英]Functional Interface Object Cast

I've encountered the following code in a Java project, and I'm not sure what to make of it: 我在Java项目中遇到了以下代码,但不确定该怎么做:

public Function<CustomEnum,String> foo(SomeObject someObject) {
    return ((Function<CustomEnum,String>) (someObject::toString)).andThen(r -> someObject::getSomethingWithEnumParameter);
}

I don't really understand how you can cast something to a Functional Interface. 我不太了解如何将某些内容投射到功能接口。 What does that even mean? 那有什么意思?

Isn't the resulting type of the return value going to be whatever value someObject. 返回值的结果类型不是someObject的任何值。

Isn't Function<CustomEnum, String> defining an anonymous function that takes a type CustomEnum and returns a String ? Function<CustomEnum, String>是否不是定义一个采用CustomEnum类型并返回String的匿名函数?

I've read the java doc for Function<T,R> , and to be honest, this doesn't make much more sense than before I read the document. 我已经阅读了Function<T,R>的Java文档,说实话,这与阅读文档之前没有多大关系。

This is what I believe is happening. 我相信这是正在发生的事情。

  1. foo is returning an anonymous function that is applied to some CustomEnum to return a String foo返回一个匿名函数,该函数应用于某些CustomEnum以返回字符串

  2. the anonymous function inside of foo (which is somehow cast onto someObject::toString , which I don't understand) is applied to the CustomEnum that will be passed from the initial call of foo(someObject).apply(customEnum) . FOO的内部匿名函数(这是某种投射到someObject::toString ,这一点我不明白)被应用到CustomEnum将从最初的呼叫传递foo(someObject).apply(customEnum)

  3. The andThen will take the resulting String from the anonymous function inside of foo (which was cast somehow I still don't understand), and then return the value of someObject::getSomethingWithEnumParameter . andThen将从foo内部的匿名函数中获取结果String(我仍然不理解它是否已被someObject::getSomethingWithEnumParameter ),然后返回someObject::getSomethingWithEnumParameter的值。 Why isn't the return type just the type of someObject::getSomethingWithEnumParameter , which we'll say is a Map<R,T> , for sake of discussion. 为什么返回类型不只是someObject::getSomethingWithEnumParameter的类型,为了便于讨论,我们将其称为Map<R,T>

If anyone could help me understand this flow, I would greatly appreciate it. 如果有人可以帮助我理解这一流程,我将不胜感激。

In an ideal world this would work: 在理想的世界中,这将起作用:

public Function<CustomEnum,String> foo(SomeObject someObject) {
    return (someObject::toString).andThen(...);
}

However Java needs an interface type in order to implicitly create an interface instance from the method reference., hence the explicit cast is required to cast to the Function interface type. 但是,Java需要接口类型,以便从方法引用隐式创建接口实例。因此,需要显式强制类型转换才能转换为Function接口类型。

Once you have an instance of Function then you can call any method on it as per normal, in this case the andThen method which composes it with another function object to form a new function. 一旦有了Function的实例,就可以按常规调用其上的任何方法,在这种情况下, andThen方法将其与另一个函数对象组成一个新函数。

Breaking it down: 分解:

someObject::toString is a method reference with implied type Function<CustomEnum, String> . someObject::toString是隐式类型为Function<CustomEnum, String>的方法引用。 Ie toString is a method on SomeObject which takes a parameter of type CustomEnum and returns a String . toString是上一个方法SomeObject这需要类型的参数CustomEnum并返回String

r -> someObject::getSomethingWithEnumParameter has the wrong type though - it's a function which returns a function. r -> someObject::getSomethingWithEnumParameter具有错误的类型-它是一个返回函数的函数。 If you get rid of the " r -> " part then it is valid, as long as someObject::getSomethingWithEnumParameter is a method on SomeObject that takes a String and returns a String . 如果你摆脱了“ r -> ”部分,那么它是有效的,只要someObject::getSomethingWithEnumParameter是一个方法SomeObject ,需要一个String ,并返回一个String Alternatively the return type of foo would need to change to Function<CustomEnum, Function<String, String>> . 或者, foo的返回类型将需要更改为Function<CustomEnum, Function<String, String>>

If you combine those two with andThen then you have a Function which takes a CustomEnum and returns a String , as pr the return type of foo . 如果将这两个与andThen结合使用,那么您将拥有一个接受CustomEnum并返回StringFunction ,作为pr的返回类型foo

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

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