I am using the gobject-introspection-1.0 library from Vala to dynamically load gir modules. As I need to call an initialization function having a fixed name, I retrieve a BaseInfo object from Repository.find_by_name.
Now, I want to invoke this function with GI.CallableInfo.invoke, which needs a GI.CallableInfo object.
Luckily, GI.CallableInfo inherits from GI.BaseInfo, and the instance I retrieve probably is a GI.CallableInfo. Therefore I try to dynamically or statically cast it :
GI.CallableInfo myCallableInfo = myBaseInfo as GI.CallableInfo;
GI.CallableInfo myCallableInfo = (GI.CallableInfo) myBaseInfo;
GI.CallableInfo myCallableInfo = (myBaseInfo is GI.CallableInfo)
? (GI.CallableInfo) myBaseInfo : null;
The first one results in a compilation error:
error: Operation not supported for this type
The second one in a runtime assertion fail, and myCallableInfo being null:
g_boxed_copy: assertion 'G_TYPE_IS_BOXED (boxed_type)' failed
The last one gives a compilation error which lead me to the compact type trail:
type check expressions not supported for compact classes, structs, and enums
How can I successfully cast a GI.BaseInfo to a GI.CallableInfo?
When you assign to an owned variable Vala has to copy the value (in this case the types aren't reference counted, so copying is the only way). The problem here is with the copying, not the casting. Assign it to an unowned variable:
unowned GI.CallableInfo myCallableInfo = (GI.CallableInfo) myBaseInfo;