It won't compile because the function expecting a reference but you passed nil
. The problem have nothing to do with optional.
By declaring parameter with inout
means that you will assign some value to it inside the function body. How can it assign value to nil
?
You need to call it like
var a : MyClass? = nil
testFunc(&a) // value of a can be changed inside the function
If you know C++, this is C++ version of your code without optional
struct MyClass {};
void testFunc(MyClass &p) {}
int main () { testFunc(nullptr); }
and you have this error message
main.cpp:6:6: note: candidate function not viable: no known conversion from 'nullptr_t' to 'MyClass &' for 1st argument
which is kind of equivalent to the on you got (but easier to understand)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…