Fn(null_object)
END
v9.CheckScriptFailure(lines, 'E1360: Using a null object', 1)
+
+ # Try to change a const object variable using a "any" variable
+ lines =<< trim END
+ vim9script
+ class A
+ public const v1: number = 123
+ endclass
+
+ def Fn(o: any)
+ o.v1 = 321
+ enddef
+
+ var a = A.new()
+ Fn(a)
+ END
+ v9.CheckScriptFailure(lines, 'E1409: Cannot change read-only variable "v1" in class "A"', 1)
+
+ # Try to change a final object variable using a "any" variable
+ lines =<< trim END
+ vim9script
+ class A
+ public final v1: number = 123
+ endclass
+
+ def Fn(o: any)
+ o.v1 = 321
+ enddef
+
+ var a = A.new()
+ Fn(a)
+ END
+ v9.CheckScriptFailure(lines, 'E1409: Cannot change read-only variable "v1" in class "A"', 1)
+
+ # Assign a different type of value to an "any" type object variable
+ lines =<< trim END
+ vim9script
+ class A
+ public var v1: list<any> = [1, 2]
+ endclass
+
+ def Fn(o: A)
+ o.v1 = 'abc'
+ enddef
+
+ var a = A.new()
+ Fn(a)
+ END
+ v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected list<any> but got string', 1)
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
// Get the current function
ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
+ ectx->ec_dfunc_idx)->df_ufunc;
+ where_T where = WHERE_INIT;
// Check whether the member variable is writeable
if ((m->ocm_access != VIM_ACCESS_ALL) &&
emsg_var_cl_define(msg, m->ocm_name, 0, cl);
status = FAIL;
}
+ // Fail if the variable is a const or final or the type
+ // is not compatible
+ else if (oc_var_check_ro(cl, m) ||
+ check_typval_type(m->ocm_type, tv, where)
+ == FAIL)
+ status = FAIL;
else
lidx = m_idx;
}