- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么是
public <R, F extends Function<T, R>> Builder<T> withX(F getter, R returnValue) {...}
public <R> Builder<T> with(Function<T, R> getter, R returnValue) {...}
withX()
喜欢
.withX(MyInterface::getLength, "I am not a Long")
The type of getLength() from the type BuilderExample.MyInterface is long, this is incompatible with the descriptor's return type: String
with()
才不是。
import java.util.function.Function;
public class SO58376589 {
public static class Builder<T> {
public <R, F extends Function<T, R>> Builder<T> withX(F getter, R returnValue) {
return this;
}
public <R> Builder<T> with(Function<T, R> getter, R returnValue) {
return this;
}
}
static interface MyInterface {
public Long getLength();
}
public static void main(String[] args) {
Builder<MyInterface> b = new Builder<MyInterface>();
Function<MyInterface, Long> getter = MyInterface::getLength;
b.with(getter, 2L);
b.with(MyInterface::getLength, 2L);
b.withX(getter, 2L);
b.withX(MyInterface::getLength, 2L);
b.with(getter, "No NUMBER"); // error
b.with(MyInterface::getLength, "No NUMBER"); // NO ERROR !!
b.withX(getter, "No NUMBER"); // error
b.withX(MyInterface::getLength, "No NUMBER"); // error !!!
}
}
javac SO58376589.java
SO58376589.java:32: error: method with in class Builder<T> cannot be applied to given types;
b.with(getter, "No NUMBER"); // error
^
required: Function<MyInterface,R>,R
found: Function<MyInterface,Long>,String
reason: inference variable R has incompatible bounds
equality constraints: Long
lower bounds: String
where R,T are type-variables:
R extends Object declared in method <R>with(Function<T,R>,R)
T extends Object declared in class Builder
SO58376589.java:34: error: method withX in class Builder<T> cannot be applied to given types;
b.withX(getter, "No NUMBER"); // error
^
required: F,R
found: Function<MyInterface,Long>,String
reason: inference variable R has incompatible bounds
equality constraints: Long
lower bounds: String
where F,R,T are type-variables:
F extends Function<MyInterface,R> declared in method <R,F>withX(F,R)
R extends Object declared in method <R,F>withX(F,R)
T extends Object declared in class Builder
SO58376589.java:35: error: incompatible types: cannot infer type-variable(s) R,F
b.withX(MyInterface::getLength, "No NUMBER"); // error
^
(argument mismatch; bad return type in method reference
Long cannot be converted to String)
where R,F,T are type-variables:
R extends Object declared in method <R,F>withX(F,R)
F extends Function<T,R> declared in method <R,F>withX(F,R)
T extends Object declared in class Builder
3 errors
import java.util.function.Consumer;
import java.util.function.Supplier;
interface TypeInference {
Number getNumber();
void setNumber(Number n);
@FunctionalInterface
interface Method<R> {
TypeInference be(R r);
}
//Supplier:
<R> R letBe(Supplier<R> supplier, R value);
<R, F extends Supplier<R>> R letBeX(F supplier, R value);
<R> Method<R> let(Supplier<R> supplier); // return (x) -> this;
//Consumer:
<R> R lettBe(Consumer<R> supplier, R value);
<R, F extends Consumer<R>> R lettBeX(F supplier, R value);
<R> Method<R> lett(Consumer<R> consumer);
public static void main(TypeInference t) {
t.letBe(t::getNumber, (Number) 2); // Compiles :-)
t.lettBe(t::setNumber, (Number) 2); // Compiles :-)
t.letBe(t::getNumber, 2); // Compiles :-)
t.lettBe(t::setNumber, 2); // Compiles :-)
t.letBe(t::getNumber, "NaN"); // !!!! Compiles :-(
t.lettBe(t::setNumber, "NaN"); // Does not compile :-)
t.letBeX(t::getNumber, (Number) 2); // Compiles :-)
t.lettBeX(t::setNumber, (Number) 2); // Compiles :-)
t.letBeX(t::getNumber, 2); // !!! Does not compile :-(
t.lettBeX(t::setNumber, 2); // Compiles :-)
t.letBeX(t::getNumber, "NaN"); // Does not compile :-)
t.lettBeX(t::setNumber, "NaN"); // Does not compile :-)
t.let(t::getNumber).be(2); // Compiles :-)
t.lett(t::setNumber).be(2); // Compiles :-)
t.let(t::getNumber).be("NaN"); // Does not compile :-)
t.lett(t::setNumber).be("NaN"); // Does not compile :-)
}
}
最佳答案
这是一个非常有趣的问题。恐怕答案很复杂。
tl;博士
找出差异需要深入阅读 Java 的 type inference specification。 ,但基本上归结为:
with
有一个(公认的含糊不清的)替换满足 R
上的所有要求:Serializable
withX
,引入附加类型参数F
强制编译器解析 R
首先,不考虑约束F extends Function<T,R>
. R
解析为(更具体的)String
这意味着 F
的推断失败。 withX
的情况下)语言设计者举起手说“在某些情况下类型推断变得太难了,所以我们只会失败”。即使编译器的行为与
withX
有关似乎是您想要的,我认为这是当前规范的附带副作用,而不是积极的设计决策。
static Runnable x = () -> System.out.println();
withX
持谨慎态度的另一个原因方法是
F
参数本身。通常,
上的泛型类型参数方法 (没有出现在返回类型中)存在以将签名的多个部分的类型绑定(bind)在一起。是说:
T
是,但要确保无论我在哪里使用
T
它是相同的类型。
F
在您的
withX
仅在签名中出现一次,这向我建议使用不符合语言此功能意图的类型参数。
with
方法最多变成 2 个链:
public class Builder<T> {
public final class With<R> {
private final Function<T,R> method;
private With(Function<T,R> method) {
this.method = method;
}
public Builder<T> of(R value) {
// TODO: Body of your old 'with' method goes here
return Builder.this;
}
}
public <R> With<R> with(Function<T,R> method) {
return new With<>(method);
}
}
b.with(MyInterface::getLong).of(1L); // Compiles
b.with(MyInterface::getLong).of("Not a long"); // Compiler error
withX
这样的无关类型参数。确实。通过将方法分解为两个签名,从类型安全的角度来看,它还可以更好地表达您要执行的操作的意图:
With
),该类定义基于方法引用的类型。 of
)约束了 value
的类型与您之前设置的内容兼容。 with
和
withX
.这一段很长,慢慢来吧。尽管很长,但我仍然遗漏了很多细节。您可能希望引用规范以获取更多详细信息(点击链接)以说服自己我是对的(我很可能犯了错误)。
Function
为
Supplier
,因此可以使用的类型和参数较少。这是一个完整的片段,可以重现您描述的行为:
public class TypeInference {
static long getLong() { return 1L; }
static <R> void with(Supplier<R> supplier, R value) {}
static <R, F extends Supplier<R>> void withX(F supplier, R value) {}
public static void main(String[] args) {
with(TypeInference::getLong, "Not a long"); // Compiles
withX(TypeInference::getLong, "Also not a long"); // Does not compile
}
}
with
with(TypeInference::getLong, "Not a long");
R <: Object
TypeInference::getLong
兼容 Supplier<R>
"Not a long"
兼容 R
R <: Object
(来自 B0)Long <: R
(来自第一个约束)String <: R
(来自第二个约束)R
成功(给出
Serializable
),则调用适用。
TypeInference::getLong
兼容 Supplier<R>
R
withX
withX(TypeInference::getLong, "Also not a long");
R <: Object
F <: Supplier<R>
TypeInference::getLong
) 不是,因为它满足以下条件:
If
m
is a generic method and the method invocation does not provide explicit type arguments, an explicitly typed lambda expression or an exact method reference expression for which the corresponding target type (as derived from the signature ofm
) is a type parameter ofm
.
"Also not a long"
兼容 R
R <: Object
(来自 B0)F <: Supplier<R>
(来自 B0)String <: R
(来自约束)R
成功(给出
String
),则调用适用。
TypeInference::getLong
兼容 F
F
F
),所以我们必须
resolve尝试此之前
reduction .所以,我们从我们的边界集 B2 开始。
V
如下:Given a set of inference variables to resolve, let
V
be the union of this set and all variables upon which the resolution of at least one variable in this set depends.
F
的分辨率取决于 R
,所以 V := {F, R}
. V
的一个子集根据规则:let
{ α1, ..., αn }
be a non-empty subset of uninstantiated variables inV
such that i) for alli (1 ≤ i ≤ n)
, ifαi
depends on the resolution of a variableβ
, then eitherβ
has an instantiation or there is somej
such thatβ = αj
; and ii) there exists no non-empty proper subset of{ α1, ..., αn }
with this property.
V
的唯一子集满足这个性质的是 {R}
. String <: R
)我们实例化 R = String
并将其合并到我们的绑定(bind)集中。 R
现在解决了,第二个界限实际上变成了 F <: Supplier<String>
. F = Supplier<String>
. F
现在已解决。 F
解决了,我们可以继续
reduction ,使用新约束:
TypeInference::getLong
兼容 Supplier<String>
Long
兼容 String
Integer <: Number
) Consumer
而不是 Supplier
)t.lettBe(t::setNumber, "NaN"); // Does not compile :-)
t.letBeX(t::getNumber, 2); // !!! Does not compile :-(
t.lettBeX(t::setNumber, 2); // Compiles :-)
withX
完全相同的推理过程。以上(只需将
Long
替换为
Number
并将
String
替换为
Integer
)。这说明了为什么您不应该在类设计中依赖这种失败的类型推断行为的另一个原因,因为在此处编译失败可能不是理想的行为。
Consumer
的调用),如果您通过为上述方法之一(即
with
)制定的类型推断程序进行工作,则行为应该很明显。对于第一个,
withX
对于第三个)。您只需要注意一个小变化:
t::setNumber
与 Consumer<R>
兼容)的约束将 reduce至 R <: Number
而不是 Number <: R
就像 Supplier<R>
一样.这在有关减少的链接文档中有所描述。 关于java - 为什么类型参数比方法参数强,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58376589/
简而言之:我想从可变参数模板参数中提取各种选项,但不仅通过标签而且通过那些参数的索引,这些参数是未知的 标签。我喜欢 boost 中的方法(例如 heap 或 lockfree 策略),但想让它与 S
我可以对单元格中的 excel IF 语句提供一些帮助吗? 它在做什么? 对“BaselineAmount”进行了哪些评估? =IF(BaselineAmount, (Variance/Baselin
我正在使用以下方法: public async Task Save(Foo foo,out int param) { ....... MySqlParameter prmparamID
我正在使用 CodeGear RAD Studio IDE。 为了使用命令行参数测试我的应用程序,我多次使用了“运行 -> 参数”菜单中的“参数”字段。 但是每次我给它提供一个新值时,它都无法从“下拉
我已经为信用卡类编写了一些代码,粘贴在下面。我有一个接受上述变量的构造函数,并且正在研究一些方法将这些变量格式化为字符串,以便最终输出将类似于 号码:1234 5678 9012 3456 截止日期:
MySql IN 参数 - 在存储过程中使用时,VarChar IN 参数 val 是否需要单引号? 我已经像平常一样创建了经典 ASP 代码,但我没有更新该列。 我需要引用 VarChar 参数吗?
给出了下面的开始,但似乎不知道如何完成它。本质上,如果我调用 myTest([one, Two, Three], 2); 它应该返回元素 third。必须使用for循环来找到我的解决方案。 funct
将 1113355579999 作为参数传递时,该值在函数内部变为 959050335。 调用(main.c): printf("%d\n", FindCommonDigit(111335557999
这个问题在这里已经有了答案: Is Java "pass-by-reference" or "pass-by-value"? (92 个回答) 关闭9年前。 public class StackOve
我真的很困惑,当像 1 == scanf("%lg", &entry) 交换为 scanf("%lg", &entry) == 1 没有区别。我的实验书上说的是前者,而我觉得后者是可以理解的。 1 =
我正在尝试使用调用 SetupDiGetDeviceRegistryProperty 的函数使用德尔福 7。该调用来自示例函数 SetupEnumAvailableComPorts .它看起来像这样:
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
rails 新手。按照多态关联的教程,我遇到了这个以在create 和destroy 中设置@client。 @client = Client.find(params[:client_id] || p
通过将 VM 参数设置为 -Xmx1024m,我能够通过 Eclipse 运行 Java 程序-Xms256M。现在我想通过 Windows 中的 .bat 文件运行相同的 Java 程序 (jar)
我有一个 Delphi DLL,它在被 Delphi 应用程序调用时工作并导出声明为的方法: Procedure ProduceOutput(request,inputs:widestring; va
浏览完文档和示例后,我还没有弄清楚 schema.yaml 文件中的参数到底用在哪里。 在此处使用 AWS 代码示例:https://github.com/aws-samples/aws-proton
程序参数: procedure get_user_profile ( i_attuid in ras_user.attuid%type, i_data_group in data_g
我有一个字符串作为参数传递给我的存储过程。 dim AgentString as String = " 'test1', 'test2', 'test3' " 我想在 IN 中使用该参数声明。 AND
这个问题已经有答案了: When should I use "this" in a class? (17 个回答) 已关闭 6 年前。 我运行了一些java代码,我看到了一些我不太明白的东西。为什么下
我输入 scroll(0,10,200,10);但是当它运行时,它会传递字符串“xxpos”或“yypos”,我确实在没有撇号的情况下尝试过,但它就是行不通。 scroll = function(xp
我是一名优秀的程序员,十分优秀!