Menu

#964 Multi-argument typemaps without variable names do not work

open
nobody
5
2024-10-24
2008-11-19
No

I want to create a typemap for two sequential argument types but I don't want to specify the variable names. The documentation states that this should be possible, but multi-argument typemaps only seem to work when variable names are given:

-----------------------------
%module multi_test;

/*
// Does not work
%typemap(default) (int &, int &) {
// FOUND
}
*/

// Works
%typemap(default) (int &a, int &b) {
// FOUND
}

class Foobar {
public:
Foobar();
void test(int &a, int &b);
~Foobar();
};
-----------------------------

BTW, I saw this also reported at the following URL but did not find a bug submitted for it:
http://thread.gmane.org/gmane.comp.programming.swig/8527/focus=8536

Discussion

  • James Masters

    James Masters - 2008-11-19

    I forgot to mention that I'm using SWIG 1.3.36.

     
  • Olly Betts

    Olly Betts - 2022-03-07

    Reproduced with current git master.

    Interestingly %typemap(default) (int &a, int &) { also works, but %typemap(default) (int &, int &b) { doesn't.

    I don't see where in the docs it explicitly says that omitting the parameter names should work though.

    I do see this text (which has been there since before 2002 it seems):

    Multi-argument typemaps are also more restrictive in the way that they are matched.
    Currently, the first argument follows the matching rules described in the previous section,
    but all subsequent arguments must match exactly.

    That actually suggests to me that %typemap(default) (int &, int &b) { would work but not %typemap(default) (int &a, int &) { (i.e. the opposite of the actual situation).

    The output with -debug-tmsearch shows the typemaps don't match when they don't match, but doesn't help me understand why not.

     

    Last edit: Olly Betts 2024-10-22
  • Olly Betts

    Olly Betts - 2024-10-22

    Reproducible with SWIG 4.3.0.

    Looking at the manual again, this part does actually seem to suggest that types without names should work:

    Each type pattern is either a simple type, a simple type and argument name, or a list of types in the case of multi-argument typemaps.

    @wsfulton Do you think this is a code bug or a documentation bug? I looked at the code and it makes my head spin, but I could fix the docs to clearly say this isn't supported (from my testing the first argument in a multi-argument typemaps currently needs to be named; for subsequent arguments names are optional).

    It seems potentially useful if you want to apply a multi argument typemap but the naming of parameters in the API being wrapped isn't consistent across functions/methods (or the prototypes don't even have parameter names) and the parameter types are distinctive enough to not occur in the same order in cases you don't want to affect. However it does also seem fairly obscure.

     
  • William Fulton

    William Fulton - 2024-10-24

    I can imagine a pile of difficulties in the implementation and ambiguities in cases such as:

    void x(int& a, int& b, int& c, int& d);
    

    and multiple variations of the multi-arg typemaps in this ticket being present. If just the first name has to match, it is unambiguous as to whether or not the multi-argument typemap matches (once the subsequent parameters are checked).

    I suppose some more flexible rules around such ambiguities could be defined and implemented, however, I would document it as it works currently and mention that currently there is a restriction that the first parameter must be named.

    I would consider patches to address the restriction, but it might be a can of worms and is low priority.

     

Log in to post a comment.

MongoDB Logo MongoDB