Comments (11)
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.
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.
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.
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.
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.
Is 0.3.0 not current? Is there a binary for which the above code will work?
from www.ziglang.org.
@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.
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.
@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.
from www.ziglang.org.
OK, thanks!
from www.ziglang.org.
Since formatting is in userspace, it should go in the stdlib docs, which are not ready yet.
from www.ziglang.org.
Related Issues (20)
- Broken links in the Overview page HOT 1
- The location of the language option needs to be changed
- Go to the specific news and then click on the options bar, you will need to select the language again
- The color scheme of the code in dark theme needs to be improved
- Link to latest master build HOT 2
- Chinese page layout problem HOT 2
- `Access-Control-Allow-Origin: *` removed from `index.json`
- Incorrect info about other languages in "why zig" comparison. HOT 1
- [autodoc] data.js served without compression, wasting ~22MB of bandwidth
- Improve `catch` documentation
- Support toggle dark mode and light mode HOT 4
- Table of Content doesn't link locally HOT 1
- Documentation change title based on current uri hash? HOT 1
- Add information or link about/to licenses of logos and mascots HOT 2
- "get started" button does not stand out HOT 2
- CI: produce new tarballs for the CI in addition to binary tarballs for the download page
- An error with the `--single-threaded` option on build
- Missing download link for zig 0.10.1 on FreeBSD HOT 2
- Zig chinese manual HOT 3
- Documentation: Clarification for pointer dereference and address of from Operators section HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from www.ziglang.org.