[英]Why does using new in a Dafny function give an error?
我想知道為什么我的以下程序出現錯誤:
class KV
{
var key : int;
var value : int;
constructor (k: int, v: int) modifies this
{
this.key := k;
this.value := v;
}
}
function foo () : KV
{
new KV(0,0)
}
我得到:運行此文件時invalid UnaryExpression
。
在Dafny function
s是純function
。 通過提供reads
子句,它們可以依賴堆。 但是它們不會產生副作用-無法修改堆。 由於函數foo
參數為零且沒有reads
子句,因此每次調用時,它必須返回相同的值。 每次調用內存分配運算符new
時,它都會給出一個不同的值,因此不能在函數中使用。
還需要注意的是,默認情況下,Dafny函數是ghost
。 它們在運行時不可執行。 而是在編譯的驗證階段使用它們。 如果要使用非虛函數,則必須編寫function method
而不是function
。
您可以在method
內部使用new
。 方法是必不可少的過程,不需要純粹。
class KV
{
var key : int;
var value : int;
constructor (k: int, v: int) modifies this
{
this.key := k;
this.value := v;
}
}
method foo () returns (kv:KV)
{
kv := new KV(0,0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.