#Am I doing something wrong or is this a bug ?

1 messages · Page 1 of 1 (latest)

torpid condor
#

So I was doing a visual programming language editor (at least started) and encountered a weird bug, for some reasons the compiler won't understand that a variable i use has a member function:

here the part of the main.zig:

const contextMenu = ContextMenu{};
...
try camera.setColor(Color.black);
try camera.clear();

try camera.setColor(BG_COLOR);
try camera.draw(.Line, bgRects);

try contextMenu.draw(&camera);

camera.present();

here the contextMenu.zig:

pub usingnamespace struct {
    const ContextMenu = @This();

    x: i32 = 0,
    y: i32 = 0,
    w: i32 = 0,
    h: i32 = 0,
    shown: bool = false,

    pub fn draw(self: *const ContextMenu, camera: *const Camera) !void {
        try camera.drawScreen(.Fill, .{
            .x = self.x,
            .y = self.y - self.h,
            .w = self.w,
            .h = self.h,
        });
    }
};

while my camera works well :/ help pwease ;(

spring condor
#

That usingnamespace seems useless to me.

#

Just do pub const ContextMenu = struct { or just put the fields at the root of the file and get rid of the struct entirely

#

I suspect ContextMenu in the top excerpt simply isn't the same as the one in the other excerpt

torpid condor
#

i did that so i can just use the type with @import

spring condor
#

That makes sense to me, because I don't think you actually could access the second one from outside the file

torpid condor
#

but i think it's that @This mess with the types

spring condor
#

Just put everything at the root of the file instead

#

If you don't do that, then the file struct != usingnamespace'd struct != that @This()

torpid condor
#

because i added an init function and now it gives me this error: expected type '*const camera', found '*camera.usingnamespace_0__struct_6232'

spring condor
#

Not the file struct it's inside

torpid condor
#

oooh i see why

spring condor
#

usingnamespace doesn't get rid of that struct, in other words

torpid condor
#

because @import gives the struct getting usingnamespaced'd with the global declarations of the file, which mess with the type

#

i figured, thanks anyway :)

spring condor
#

No; the contents of a file are as if they are inside a struct - and that struct is what @import returns

torpid condor
#

i know that

spring condor
#

The usingnamespaced struct in that example is inside that struct

torpid condor
#

yeah it merges all declarations and fields to the struct "created" by @import