why my interface is sealed in maxon::Result?
-
Hi guys,
I'm following the maxonsdk.module listed in here. Which declare aSimpleNumberInterface
and implement it withSimpleNumberImpl
. I mimic the steps as follow- projectdefinition.txt
Platform=Win64;OSX;Linux Type=DLL APIS=core.framework stylecheck=false ModuleId=net.yu.myp
- myifc.h
#pragma once #include "maxon/object.h" class MyIfc : MAXON_INTERFACE_BASES(maxon::ObjectInterface) { MAXON_INTERFACE(MyIfc,MAXON_REFERENCE_NORMAL,"net.yu.interface.myifc"); public: MAXON_METHOD void Hello(); }; #include "myifc1.hxx" MAXON_DECLARATION(maxon::Class<MyIfcRef>, MyIfcDecl, "net.yu.myifcdecl"); #include "myifc2.hxx"
- myifc.cpp
#include "myifc.h" class MyIfcImpl : public maxon::Component<MyIfcImpl,MyIfc> { MAXON_COMPONENT(); public: MAXON_METHOD void Hello() { } }; MAXON_COMPONENT_CLASS_REGISTER(MyIfcImpl, MyIfcDecl);
- main.cpp
#include "myifc.h" extern "C" __declspec(dllexport) int c4d_main(int action, void* p1, void* p2, void* p3) { MyIfcRef ifc = MyIfcDecl().Create(); // A return 1; }
At line A , I got the following error
error C2440: 'initializing': cannot convert from 'maxon::Result<MyIfcRef>' to 'MyIfcRef'
How does this happen? Many thanks!
-
This post is deleted! -
Hello @yushang,
thank you for reaching out to us. Please have a look at our Error System documentation. You are missing an
iferr_return
and an error scope in your code.int c4d_main(int action, void* p1, void* p2, void* p3) { // Errors imply being propagated upwards in a call chain. When we want to terminate such chain, // i.e., have this function return `int` (you should use Int32 as int is ambiguous) instead of // `maxon::Result<maxon::Int32>` we can use a custom error handler to handle errors being thrown. // When the return type is `maxon::Result<T>` we can just use an `iferr_scope;` instead. iferr_scope_handler { DebugOutput("c4d_main failed with '@'", err); return 0; }; // Create methods return a `maxon::Result<T>` object, i.e., either the value or the error. The // `iferr_return` keyword will unpack that error for us. MyIfcRef ifc = MyIfcDecl().Create() iferr_return; // Technically possible, but bad code: maxon::Result<MyIfcRef> result = MyIfcDecl().Create(); // Can make sense in some cases, but should be generally avoided: MyIfcRef ifc = MyIfcDecl().Create() iferr_ignore("We don't care about the error here."_s); // Finally, there is a special form of branching that allows to handle errors in a more detailed way: ifnoerr (MyIfcRef ifc = MyIfcDecl().Create()) { // Runs when the creation was successful. } iferr (MyIfcRef ifc = MyIfcDecl().Create()) { // Would run when the creation failed. } return 1; }
Cheers,
Ferdinand -
@ferdinand cool. this really helpful!