Im having the following in my Swig interface file interfacing a .c/.h for a small GUI library:
%{
void Widget_name_set( Widget *widget, char *name )
{
if( !name ) return;
WidgetSetName( widget, name );
}
char *Widget_name_get( Widget *widget )
{
return WidgetGetName( widget );
}
%}
struct Widget
{
%extend
{
char name[ 32 ];
Widget( void )
{
return WidgetNew();
}
~Widget()
{
if( $self ) WidgetDelete( $self );
}
void SetName( char *name )
{
Widget_name_set( $self, name );
}
char *GetName()
{
return Widget_name_get( $self );
}
}
};
Then I use the interface file to generate a Lua wrapper. The results are almost as expected if I call in Lua the following:
w = Widget();
w:SetName("test");
Everything is ok. But if I do this:
w = Widget();
w.SetName( nil, "test" );
It obviously crash as the parameter is nil. Is there a way (either using the Swig interface or in Lua) to suppress all the call with a dot and only keep the one with column? Like this it would be easier for the user and will avoid me to add 10 million checks of pointers etc…
No. It’s just semantic sugar as far as Lua is concerned. Using
.means to just access the member. Using:means that when you call the function, the first parameter is the table being accessed with:.You probably couldn’t even tell the difference from inspecting the Lua disassembly (though admittedly, that’s just guessing on my part); by then, it’s probably been converted into its final form.