无法确定的条件前pression的类型,因为有'廉政'和&lt之间不存在隐式转换;零&GT; [英] Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>
问题描述
为什么这个不能编译?
诠释?数=真的吗? 5:空;
无法确定的条件前pression类型,因为有'廉政'和&lt之间不存在隐式转换;零&GT;
块引用>解决方案该规范(§7.14)说,对于有条件的前pression
B? X:Y
,有三种可能性,无论是X
和是
都有一个类型和的一定的良好的条件的满足,只有的一个X
和是
有一个类型的和的一定的良好的条件的满足,或者发生编译时错误。在这里,一定利好条件是指一定的转换是可能的,我们将进入下面的详细信息。现在,让我们转向规范的锗部分:
如果只有一个
X
和是
有一个类型,都X
和是
的隐式转换为该类型,然后就是有条件的前pression的类型。
块引用>这里的问题是,在
诠释?数=真的吗? 5:空;
仅在条件结果中的一个具有一个类型。在这里,
X
是INT
文字和<$ C $ C>是是空
这确实的不的有型和空
不是隐式转换为一个INT
1 。因此,一定的良好条件不符合要求,并发生编译时错误。有是的解决此两种方式:
诠释?数=真的吗? (INT?)5:空;
下面我们仍然在只有
X
和是
人有一个类型的情况。需要注意的是空
的还是的没有一个类型尚未编译器将不会有任何问题,这一点,因为(INT? )5
和空
都是隐式转换为INT?
(第6.1.4节和第6.1.5节)。另一种方法是明显:
诠释?数=真的吗? 5:(INT?)空;
但现在我们要读的不同的的的规范条款,明白这是为什么好:
如果
X
已键入X
和是
有键入是
然后
如果(第6.1节)从
X
存在ÿ的隐式转换
,但不能从是
到X
,那么是
是有条件的类型前pression。
如果(第6.1节)从
是
存在X
,而不是从一个隐式转换X
到是
,那么X
是有条件的类型前pression。
否则,没有前pression类型可确定,并发生编译时错误。
块引用>下面
X
的类型为INT
和是
是键入INT?
的。有一个从INT?
来的隐式转换INT
,但是从的隐式转换INT
到INT?
所以前pression的类型是INT?
1 :进一步注意,左手侧类型在确定的条件前pression,混乱的一个共同的源此处的类型被忽略。
Why does this not compile?
int? number = true ? 5 : null;
Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>
解决方案The spec (§7.14) says that for conditional expression
b ? x : y
, there are three possibilities, eitherx
andy
both have a type and certain good conditions are met, only one ofx
andy
has a type and certain good conditions are met, or a compile-time error occurs. Here, "certain good conditions" means certain conversions are possible, which we will get into the details of below.Now, let's turn to the germane part of the spec:
If only one of
x
andy
has a type, and bothx
andy
are implicitly convertible to that type, then that is the type of the conditional expression.The issue here is that in
int? number = true ? 5 : null;
only one of the conditional results has a type. Here
x
is anint
literal, andy
isnull
which does not have a type andnull
is not implicitly convertible to anint
1. Therefore, "certain good conditions" aren't met, and a compile-time error occurs.There are two ways around this:
int? number = true ? (int?)5 : null;
Here we are still in the case where only one of
x
andy
has a type. Note thatnull
still does not have a type yet the compiler won't have any problem with this because(int?)5
andnull
are both implicitly convertible toint?
(§6.1.4 and §6.1.5).The other way is obviously:
int? number = true ? 5 : (int?)null;
but now we have to read a different clause in the spec to understand why this is okay:
If
x
has typeX
andy
has typeY
then
If an implicit conversion (§6.1) exists from
X
toY
, but not fromY
toX
, thenY
is the type of the conditional expression.If an implicit conversion (§6.1) exists from
Y
toX
, but not fromX
toY
, thenX
is the type of the conditional expression.Otherwise, no expression type can be determined, and a compile-time error occurs.
Here
x
is of typeint
andy
is of typeint?
. There is no implicit conversion fromint?
toint
, but there is an implicit conversion fromint
toint?
so the type of the expression isint?
.1: Note further that the type of the left-hand side is ignored in determining the type of the conditional expression, a common source of confusion here.
这篇关于无法确定的条件前pression的类型,因为有'廉政'和&lt之间不存在隐式转换;零&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!