immutable auto a = Array!int([1, 2, 3]);
Error: cannot implicitly convert expression (((Array!int __slArray2557 = Array(RefCounted(RefCountedStore(null)));) , __slArray2557).this([1, 2, 3])) of type Array!int to immutable(Array!int)創建用戶定義的不可變對象
通常我只是想在運行時創建一些對象,初始化它,然後讓它不變,但如果我嘗試這樣做,我從上面得到的錯誤。
看來,我可以投一個可變對象不可變對象
immutable auto a = cast(immutable Array!int) Array!int([1, 2, 3]);
爲什麼我必須將它轉換爲immutable
?
正在鑄就永恆的合法嗎?
我幾乎對D幾乎一無所知,但是如果你將變量聲明爲不可變的,編譯器可能會期望右邊的表達式是不可變的對象,所以當然如果不是這種情況,它可能會給你一個錯誤。關於合法與否,我不知道,但如果您沒有收到答覆,我建議您在官方網站的D論壇之一上提出同樣的問題。 – nbro