无法确定的条件前pression的类型,因为有'廉政'和&lt之间不存在隐式转换;零> [英] Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>

查看:185
本文介绍了无法确定的条件前pression的类型,因为有'廉政'和&lt之间不存在隐式转换;零>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个不能编译?

 诠释?数=真的吗? 5:空;


  

无法确定的条件前pression类型,因为有'廉政'和&lt之间不存在隐式转换;零>



解决方案

该规范(§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, either x and y both have a type and certain good conditions are met, only one of x and y 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 and y has a type, and both x and y 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 an int literal, and y is null which does not have a type and null is not implicitly convertible to an int1. 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 and y has a type. Note that null still does not have a type yet the compiler won't have any problem with this because (int?)5 and null are both implicitly convertible to int? (§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 type X and y has type Y then

  • If an implicit conversion (§6.1) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.

  • If an implicit conversion (§6.1) exists from Y to X, but not from X to Y, then X is the type of the conditional expression.

  • Otherwise, no expression type can be determined, and a compile-time error occurs.

Here x is of type int and y is of type int?. There is no implicit conversion from int? to int, but there is an implicit conversion from int to int? so the type of the expression is int?.

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆