Skip to content
This repository was archived by the owner on Dec 27, 2021. It is now read-only.

does it work at comptime? #1

Open
andrewrk opened this issue Dec 13, 2018 · 2 comments
Open

does it work at comptime? #1

andrewrk opened this issue Dec 13, 2018 · 2 comments

Comments

@andrewrk
Copy link

One thing I'd like to consider is if the pattern works at comptime. I know that the Random interface at least works at comptime. Does this proof of concept work at comptime?

(If not, is it something that can be improved in the compiler, and it will work fine? I'm willing to do that work if necessary)

@Hejsil
Copy link
Owner

Hejsil commented Dec 13, 2018

My current prototype of vtable.populate does a cast from fn (self: *T) K -> fn (self: *c_void) K when it has validated that this is "safe" (I'm not sure if a do all the right tests). I do this, as I have no way of generating a wrapper function at comptime that casts the *c_void -> *T and calls the actual function.

Sadly, this does not work at comptime:

/home/hejsil/repo/tm35-gen4/test.zig:36:45: error: expected type '*MemReader', found '*c_void'
            return reader.vtable.read(reader.impl, buf);
                                            ^
/home/hejsil/repo/tm35-gen4/test.zig:70:34: note: called from here
    const bytes = try reader.read(buf[0..]);
                                 ^
/home/hejsil/repo/tm35-gen4/test.zig:6:20: note: called from here
        while (byte(reader)) |c| {
                   ^
/home/hejsil/repo/tm35-gen4/test.zig:36:45: note: pointer type child 'c_void' cannot cast into pointer type child 'MemReader'
            return reader.vtable.read(reader.impl, buf);
                                            ^
/home/hejsil/repo/tm35-gen4/test.zig:41:19: note: MemReader declared here
const MemReader = struct {

Seems like comptime sees through my cast of the function, but not the cast from *T -> *c_void.

@andrewrk
Copy link
Author

Seems like comptime sees through my cast of the function, but not the cast from *T -> *c_void.

I think this can be fixed without too much changes, let me take a look. Zig is getting smarter about pointer casting at comptime: ziglang/zig@b883bc8#diff-d03d33b1588c4f305b248019071b9546R13

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants