Giter VIP home page Giter VIP logo

Comments (11)

tiehuis avatar tiehuis commented on May 27, 2024

The current api is:

const std = @import("std");

pub fn main() !void {
    // Returns a `File` corresponding to stdout.
    const stdout_file = try std.io.getStdOut();

    // Get the `FileOutStream` corresponding to stdout.
    var stdout_stream = stdout_file.outStream();

    // Bind the generic `OutStream` field of `FileOutStream` which contains
    // all the formatted output functions.
    var stdout = &stdout_stream.stream;

    try stdout.print("Hello, {}\n", "world");
}

from www.ziglang.org.

Keith-S-Thompson avatar Keith-S-Thompson commented on May 27, 2024

I'm currently using zig 0.2.0, and having some trouble building a more current version (I think because I have LLVM 6.0 and it requires 7.0).

The above code gives me:

hello3.zig:8:36: error: no member named 'outStream' in struct 'File'
    var stdout_stream = stdout_file.outStream();
                                   ^

Is there a way to do with with zig 0.2.0?

from www.ziglang.org.

andrewrk avatar andrewrk commented on May 27, 2024

0.2.0 is not supported. The current release is 0.3.0, or better yet, use master branch.

If you're having trouble building, why not use one of the static binaries?

from www.ziglang.org.

Keith-S-Thompson avatar Keith-S-Thompson commented on May 27, 2024

I just tried the static binary (zig-linux-x86_64-0.3.0.tar.xz).

With your code above:

$ zig version
0.3.0
$ zig run hello.zig
/home/kst/hello.zig:8:36: error: no member named 'outStream' in struct 'File'
    var stdout_stream = stdout_file.outStream();
                                   ^
$

strace indicates that it is able to find files under lib/zig in the directory where I unpacked the tarball.

This is on Ubuntu 18.04.

(It's entirely possible that I'm doing something wrong.)

from www.ziglang.org.

andrewrk avatar andrewrk commented on May 27, 2024

If you use 0.3.0 you need to refer to the 0.3.0 documentation. The code above is from the current API.

from www.ziglang.org.

Keith-S-Thompson avatar Keith-S-Thompson commented on May 27, 2024

Is 0.3.0 not current? Is there a binary for which the above code will work?

from www.ziglang.org.

bfloch avatar bfloch commented on May 27, 2024

@Keith-S-Thompson Were you able to resolve the issue? Your code sample works for me:

$ zig version
0.3.0+b953e322

from www.ziglang.org.

Russtopia avatar Russtopia commented on May 27, 2024

The current api is:

const std = @import("std");

pub fn main() !void {
    // Returns a `File` corresponding to stdout.
    const stdout_file = try std.io.getStdOut();

    // Get the `FileOutStream` corresponding to stdout.
    var stdout_stream = stdout_file.outStream();

    // Bind the generic `OutStream` field of `FileOutStream` which contains
    // all the formatted output functions.
    var stdout = &stdout_stream.stream;

    try stdout.print("Hello, {}\n", "world");
}

Hi, as a new 'ziglanger' I found the above extremely useful -- but I would like to wrap this setup into a helper function rather than put such basic boilerplate inline at the start of every program's main().

However I'm having issues figuring out how to tell zig properly to declare the variables as having global scope or to otherwise return the stream handle properly:

pub fn stdOut() !*os.File.OutStream.Stream {
    const f: os.File = try std.io.getStdOut();
    var fos: os.File.OutStream = f.outStream();
    var fs: *os.File.OutStream.Stream = &fos.stream;

    try fs.print("Printing from stdOut: {}\n", "success"); // This works...

    return fs;
}

pub fn main() !void {
    var stdout = try stdOut();
    try stdout.print("Foo\n");  // .. but this segfaults
}

I suspect it's a scope issue -- that one of os.File or os.File.OutStream need to outlive the return of my stdOut() helper function, but I can't seem to figure out how to declare them outside of the helper stdOut() as globals, or how to return them properly to main().

// How to call this in a global context?
const f: os.File = try std.io.getStdOut(); // error: return expression outside function definition

I've tried setting 'f' to type std.os.WindowsGetStdHandleErrs!std.os.file.File, or io.GetStdIoErrs!File, but that's private so I can't seem to declare what I need at all in a global context here.

Any ideas?

from www.ziglang.org.

tiehuis avatar tiehuis commented on May 27, 2024

@Russtopia, you will need to declare the variable as a global, and then initialize later on program entry. You are unable to initialize like you are trying because opening a file cannot be evaluated at comptime and all top-level const declarations are implicitly comptime.

An example of how to do this can be found in the self-hosted compiler. This initializes stderr and stdout in main and uses them as needed in all other functions.

https://github.com/ziglang/zig/blob/163a8e98bc04ea955ee54d5905436ffac34c93a2/src-self-hosted/main.zig#L57

from www.ziglang.org.

Russtopia avatar Russtopia commented on May 27, 2024

OK, thanks!

from www.ziglang.org.

kristoff-it avatar kristoff-it commented on May 27, 2024

Since formatting is in userspace, it should go in the stdlib docs, which are not ready yet.

from www.ziglang.org.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.