Skip to content

Impossible to return a pointer in an Union #269

Open
@krichprollsch

Description

@krichprollsch

I would like be able to return a pointer to an object via the Union.

Example with html/Window from https://github.com/lightpanda-io/browser, the following Union declaration:

const generate = @import("../generate.zig");                                     
pub const Interfaces = generate.Tuple(.{                                         
    Window,                                                                      
});                                                                              
const Generated = generate.Union.compile(Interfaces);                            
pub const Union = Generated._union;                                              
pub const Tags = Generated._enum; 

Adding this function doesn't compile.

    pub fn get_window(self: *Window) Union {                                     
        return .{ .Window = self };                                            
    }  
$ zig build
install
└─ install lightpanda
   └─ zig build-exe lightpanda Debug native 1 errors
src/html/window.zig:81:29: error: expected type 'html.window.Window', found '*html.window.Window'
        return .{ .Window = self };
                            ^~~~
src/html/window.zig:43:20: note: struct declared here
pub const Window = struct {
                   ^~~~~~
referenced by:
    callFunc__anon_29998: vendor/zig-js-runtime/src/engines/v8/generate.zig:916:22
    getter: vendor/zig-js-runtime/src/engines/v8/generate.zig:1029:21
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

But this one works.

    pub fn get_window(self: *Window) Union {                                     
        return .{ .Window = self.* };                                            
    }  

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions