//generic queue data object
class QNode<T> {
var key: T? = nil
var next: QNode? = nil
}
public class Queue<T> {
//Q1
private var top: QNode<T>! = QNode<T>()
//enqueue the specified object
func enQueue(var key: T) {
//check for the instance
if (top == nil) {
//Q2
top = QNode()
}
//establish the top node
if (top.key == nil) {
top.key = key
return
}
var childToUse: QNode<T> = QNode<T>()
var current: QNode = top
//cycle through the list of items to get to the end.
while (current.next != nil) {
current = current.next! }
//append a new item
childToUse.key = key
current.next = childToUse
}
}
如果将Q1更改为private var top:QNode! = QNode()将提示错误&#34;泛型参数的参数&#39; T&#39;无法推断&#34;,但在原始代码Q2中工作正常没有错误?
答案 0 :(得分:1)
这里有一些有趣的行为,因为虽然类型推断确实如此:
var top: QNode<String> = QNode<String>()
与写作相同
var top: QNode = QNode<String>()
或确实
private var top: QNode! = QNode<T>()
隐式展开使用!在编译器中创建了一些不寻常的行为,尽管它告诉我们它解释了
QNode<T>!
如果我们点击变量名称,那么属于private var top: QNode<T>! = QNode<T>()
类型,实际上它的行为与:
private var top: QNode<T>! = QNode()
或
QNode!
编译器中的这种混淆在我看来就像一个错误,因为它应该将QNode<T>!
视为QNode<T>!
或完全拒绝它。幸运的是,只要使用echo $this->Form->input('city_name', array('name' => 'City[][city_name]'));
echo $this->Form->input('city_latitude', array('name' => 'City[][city_latitude]'));
echo $this->Form->input('city_longitude', array('name' => 'City[][city_longitude]'));
,就可以了解这个错误。
我也向Apple提交了这个错误,虽然我不能单独复制这个错误,但是当我有机会时会仔细查看。