Handle C typedef on different platform using NativeCall

I don't think so. But since $*DISTRO.is-win and $*KERNEL.bits are effectively constants, you can move the decision on which version to use to compile time:

sub handle_foo32(int32 is rw) is native(Str) { * }
sub handle_foo64(int64 is rw) is native(Str) { * }
sub handle_foo00(long  is rw) is native(Str) { * }

my constant &handler = $*DISTRO.is-win
  ?? $*KERNEL.bits == 32
    ?? &handle_foo32
    !! &handle_foo64
  !! &handle_foo00;

Except this won't of course help you with the differently sized parameters for each version. So I guess you would need another layer of indirection for that.

Perhaps as inspiration, here's how I handled similar issues: https://github.com/lizmat/P5getpwnam/blob/master/lib/P5getpwnam.pm6


Perhaps the most direct translation is to use constant to introduce a new symbol, computed at compile time.

my constant foo_t = do if $*DISTRO.is-win {
    given $*KERNEL.bits {
        when 32 { int32 }
        when 64 { int64 }
    }
} else {
    long
}

A constant bound to a type object acts just like a type, and can be used in the signature.

sub handle_foo(foo_t is rw) is native { * }

Tags:

C

Raku

Nativecall