Skip to content

A way to pass "self" parameter without reference on C imported methods. #9

Answered by edubart
Andre-LA asked this question in Q&A
Discussion options

You must be logged in to vote

When using :, the self argument is always declared as a pointer to the object, so this code:

function Vector2:Add(v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end

is really a shortcut for:

function Vector2.Add(self: *Vector2, v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end

As you can see is wrong, because the imported C function doesn't expect a pointer in the v1 argument.
In this case you need to explicitly tell the first argument, so like in lua use . instead of :, do this:

function Vector2.Add(v1: Vector2, v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end

then the x:Add(x2) should work as expected.

Plus TIP: On the very last commit I've implemented binary operator …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by edubart
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #9 on December 24, 2020 13:52.