If at the perl level your Bar and Baz also inherit from Foo, the call will get accepted in fact. Now if at the C level calling getName will also work on e.g. bar objects even though they are currently in a foo typed variable, then all is fine (depends on how the C level inheritance is done, usually this would work).

But even if it doesn't, you could always build your own polymorphic GetName by simply saying the argument is an SV * and determining the exact type by yourself with something like (untested):

... GetName(SV *thingy) PREINIT: IV tmp; CODE: if (sv_derived_from(thingy, "Foo")) { fooRef foo; tmp = SvIV((SV*) SvRV(thingy)); foo = INT2PTR(fooRef, tmp); getName(foo); } else if (sv_derived_from(thingy, "Bar")) { barRef bar; tmp = SvIV((SV*) SvRV(thingy)); bar = INT2PTR(barRef, tmp); getName(bar); } else if (sv_derived_from(thingy, "Baz")) { bazRef baz; tmp = SvIV((SV*) SvRV(thingy)); baz = INT2PTR(bazRef, tmp); getName(baz); } else croak("Some unhandled argument type message"); ...

Doing it like that is not necessarily a good idea though. If Foo, Bar and Baz form the basis of separate object hierarchies at the perl level, it probably makes more sense to have a GetName for each object type in your XS code. No need to pollute the accessor name with the type though, just have them all in a different MODULE/PACKAGE section.


In reply to Re: XSUBs with variable input types by thospel
in thread XSUBs with variable input types by crenz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.