#Is this a good project to use to learn Zig?

1 messages · Page 1 of 1 (latest)

north silo
#

I'm coming from F# with 25 years of GC programming experience, and am wondering if this is a good idea. I'm very familiar with a 3D application's C-style interface for interop (python, .net, etc). I have an idea for a native plugin, but cpp is required. You have to derive and override a parent command like so:

class SampleCommand : public CCommand
{
public:
  const wchar_t* EnglishCommandName() override { return L"SampleCommand"; }
  CCommand::result RunCommand(const CCommandContext& context) override;
  
};

I thought this might be a good excuse to learn Zig, but I don't know the pain required to do everything through this cpp boilerplate wrapper. I'm pretty sharp, but I'm not looking to have my intelligence put on trial by fire. Avoiding cpp for a quarter century was not an accident.

knotty depot
north silo
#

Zig would be editing 3D (nurbs) objects by calling a C-style library

#

but Zig would be called by a boilerplate cpp wrapper cause inheritance is how the native plugin system works

knotty depot
#

Zig has excellent C interop that actually feels nice. With dedicated types and keyword for C ABI compatibility. Definitely anything C related is a good use case for zig

#

And C++ of course can call C libraries, which zig essentially can produce.

#

You would need to write the headers yourself though, there is an open issue with the auto header generation as far as I remember.

north silo
#

the headers for the Zig library for cpp to link to?

knotty depot
#

Yes

north silo
#

ok. I feel like the cpp wrapper could be code gen'd

knotty depot
#

theere is an -femit-h that doesn't work currently sadly

north silo
#

it's very straightforward

#

can comptime do code gen?

knotty depot
north silo
#

Maybe there's another mechanism? In .NET, you can tag a method with a custom attribute, then use reflection and generate things automatically.

knotty depot
north silo
#

I read the first article. I thought you were meaning to use comptime to do my own code gen, but then it* says comptime can't touch IO?

knotty depot
#

Personally I've enjoying how zig handles opaque types. For examples in this WIP project I have to create bindings for a C lib (R), the main object being passed around is opaque, and there are specific function to coerce primitive types to the opaque one. So instead of having to know which one to call, I have this generic function that does simple type reflection and calls the right function for you depending on the type passed, with useful compilation error in case you get it wrong:

pub fn asScalarVector(from: anytype) Robject {
    const T = @TypeOf(from);

    const out = switch (T) {
        f64 => r.Rf_ScalarReal(from),
        c_int => r.Rf_ScalarInteger(@intCast(from)),
        bool => r.Rf_ScalarLogical(@intCast(@intFromBool(from))),
        else => blk: {
            switch (@typeInfo(T)) {
                .Float, .ComptimeFloat => {
                    break :blk r.Rf_ScalarReal(@floatCast(from));
                },
                .Int, .ComptimeInt => {
                    if (from > math.maxInt(c_int)) {
                        errors.stop("Number is larger than 32-bit integer can represent. Max: {d}, found: {d}", .{ math.maxInt(c_int), from });
                    }

                    if (from < math.minInt(c_int)) {
                        errors.stop("Number is smaller than 32-bit integer can represent. Min: {d}, found: {d}", .{ math.minInt(c_int), from });
                    }

                    break :blk r.Rf_ScalarInteger(@intCast(from));
                },
                else => @compileError("Attempting to coerce unsupported type"),
            }
        },
    };

    return out;
}
knotty depot
north silo
#

it sounds like I will have to get a grasp on c headers first, cause I will have to manually keep them and the cpp wrapper in sync by hand.

knotty depot
#

Looking at the latest comments there might be a workaround to have the headers generated automatically. Haven't tried it though.

north silo
#

Ok, I will look at that as well.

knotty depot
# north silo Ok, I will look at that as well.

-ofmt=c workaround does seem to work, but it is a bit of a mess. It outputs a C file you can extract your declarations from. But you also would need to copy zig.h that comes with zig (in lib directory).