ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.59k stars 2.53k forks source link

If you set a function public, all the input and output parameter type must be public, or you will get a compile error #1183

Open bronze1man opened 6 years ago

bronze1man commented 6 years ago

I have saw the part of the source code in the std/io.zig

pub fn getStdOut() GetStdIoErrs!File {
    const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_OUTPUT_HANDLE) else if (is_posix) os.posix.STDOUT_FILENO else unreachable;
    return File.openHandle(handle);
}

I think the return type is like !std.io.File so I write follow code:

fn getStdOut() !std.io.File{
 return std.io.getStdOut();
}

But it is not work:

xxx/h.zig:11:23: error: 'File' is private
fn getStdOut() !std.io.File{
                      ^
/usr/local/zig/build/lib/zig/std/io.zig:13:1: note: declared here
const File = std.os.File;

So I have to change the code like this(it works):

fn getStdOut() !std.os.File{
 return std.io.getStdOut();
}

So I have follow proposal:

This proposal will make reading code easier.

codehz commented 4 years ago

btw, what's about type alias, I mean

const Alias = fn () void;

pub fn registerHandler(al: Alias) void {
    unreachable;
}

should it emit a compiler error in those case?

Hadron67 commented 3 years ago

What about types in comptime closures?

pub fn Foo(comptime T: type) type {
    const A = struct { ptr: *T }; // no way to access A from outside...
    return struct {
        val: T,
        pub fn getPtr(self: *@This()) A {
            return .{ .ptr = &self.val };
        }
    };
}
lisael commented 1 year ago

@Hadron67 It can easily be expressed with

pub fn Foo(comptime T: type) type {
    return struct {
        pub const A = struct { ptr: *T };
        val: T,
        pub fn getPtr(self: *@This()) A {
            return .{ .ptr = &self.val };
        }
    };
}

Am I missing something ?