#What would you think of @import options

1 messages · Page 1 of 1 (latest)

wide knoll
#

I'm been thinking recently about how when zig imports something it basically just takes the file and wraps it in a struct then namespaces it with the name of the variable followed by a .. This of course is very annoying with things like SDL as it ends up being sdl.SDL_Init. Well, what if there was a way to tell @import to manually remove the redundent SDL_ from the syntax. Something like @import("sdl_bindings.zig", .{ .namespace = "SDL", .seperator = "_" })

then you could do something like sdl.Init

little moon
#

surely sdl_bindings.zig can choose better names for its exports so this isn't necessary

abstract swallow
#

I just directly include libraries with cImport

#

I have not run into issues with it.

#

This way you get names like c.SDL_Init

#

and you can understand the code without needing to look at binding specific docs

wicked shell
#

Cool idea, but the solution is not to add what could be considered bloat in order to make up for bad choices for function names.

sly spire
#

if that's implemented, I think it would be better to implement as a translate-c flag

small cloud
#

If you have control over the sdl_bindings file you can do something like:

extern "SDL2" fn SDL_Init(flags: u32) c_int;
pub const init = SDL_Init;

Then you can use the function with sdl.init(flags) and the LSP even recognizes that it is an alias and shows you the original declaration if you mouse over it.

kindred axle
#

if you do want to generate it, you could use a translate-c step as well as a Run step that takes the result of the translate-c step and creates a zig file that you can import

dusky bronze
#

you can do this with comptime, you just loop through the decls in the @import struct and replace the names

#

and create a new struct

kindred axle
#

oh that’s really smart actually, that’d probably be a lot more convenient too

#

unfortunately cant be cached though :/

dusky bronze
#

can't it? I think the build system is smart enough to know when sdl_bindings changes

kindred axle
#

well comptime isnt part of the build system

#

comptime code is reinterpreted each time
theres no caching for it

dusky bronze
#

I see I see

thorny pine
#

Comptime also cant generate decls only fields

dusky bronze
#

that's just misinformation lol

kindred axle
#

since you dont care about method syntax, you could just store each function as a field

kindred axle
kindred axle
thorny pine
#

It would be nice if @Type could at least take in existing decls and only change name or smth

dusky bronze
#

my whole life is a lie 😐

kindred axle
#

I dont think it needs to change

tardy dagger
#

What's the decls field on the typeInfo struct for structs for then?

thorny pine
#

I get why andrew is afraid of allowing decl generation, but sometimes it could be useful i guess 😅

kindred axle
#

@Type kinda just throws it away afaik
might use it for memoization though or whatever

tardy dagger
#

Ah okay

thorny pine
#

But for this topic the answer imo is forking the sdl_binding.zig and editing to your needs

#

Or usingnamespace

kindred axle
#

id honestly just copy the cimport and then :s/SDL_//g every line that doesnt start with extern

#

idk how portable that is though

#

I guess doing that in a translate-c step + Run step wouldnt be bad at all though

#

although zig doesnt have regex support so it might not work
and there might be some edge cases too youd need to worry about

#

overall kinda bleh

thorny pine
#

Id do code generation in build.zig or offline

dusky bronze
#

ok proof of concept tho:

const imported = struct {
    pub const SDL_abc: i32 = 420;
    pub const SDL_xyz: f32 = 69;
};

const fixed = fix: {
    const decls = @typeInfo(imported).Struct.decls;
    var fields: [decls.len]std.builtin.Type.StructField = undefined;
    for (decls, &fields) |decl, *field| {
        const value = @field(imported, decl.name);
        field.* = .{
            .name = decl.name[4..],
            .type = @TypeOf(value),
            .default_value = @ptrCast(&value),
            .is_comptime = true,
            .alignment = @alignOf(@TypeOf(value)),
        };
    }

    break :fix @Type(.{ .Struct = .{
        .layout = .auto,
        .fields = &fields,
        .decls = &.{},
        .is_tuple = false,
    } }){};
};
#

this does indeed work

thorny pine
#

Though you are converting a decl into a field?

dusky bronze
#

it's a comptime known value

kindred axle
#

yeah now youd need to do like
const sdl = @import(“sdl.zig”){} or something

#

or const sdl: @import(“sdl.zig”) = .{} lol

dusky bronze
#

nope it's instantiated once

thorny pine
#

Also iirc typeinfo cant be done on cimported struct 🥹

dusky bronze
#
pub fn main() !void {
    std.debug.print("{} {}\n", .{ fixed.abc, fixed.xyz });
}
kindred axle
thorny pine
thorny pine
kindred axle
#

would it maybe force-evaluate every decl/field?
cimport sometimes has compile errors for some decls if it cant translate so maybe that ciould cause issues

kindred axle
#

I wonder how slow it would be though
youll need to collect all of the decls that start with SDL_ and rename them
it could take a long time with comptime

dusky bronze