--- /dev/null
+[submodule "examples"]
+ path = examples
+ url = https://github.com/onyx-lang/onyx-examples
-Subproject commit 1df16a331e64f15a5095d2e79dbc2dcece461382
+Subproject commit 45ff12e45a39d7b0f6afff99cf8101e1a35d6c73
}
reg: otmp.TemplateRegistry;
-#inject Res {
- render :: (r: &Res, template: str, vars: any) {
- m := misc.any_to_map(vars) ?? make(Map(str, any));
- defer delete(&m);
+Res.render :: (r: &Res, template: str, vars: any) {
+ use m := misc.any_to_map(vars) ?? make(Map(str, any));
- s := reg->render_template(template, &r.writer, &m);
+ s := reg->render_template(template, &r.writer, &m);
- if s != .None {
- log(.Warning, "Template Renderer", tprintf("{}", s));
- }
-
- r.headers["Content-Type"] = "text/html";
- r->status(200 if s == .None else 400);
- r->end();
+ if s != .None {
+ log(.Warning, "Template Renderer", tprintf("{}", s));
}
+
+ r.headers->put("Content-Type", "text/html");
+ r->status(200 if s == .None else 400);
+ r->end();
}
main :: () {
release_resource: (res: &Res) -> void;
}
-#inject Cached_Resource {
- get :: (use self: &#Self) -> ? self.Res {
- if !resource {
- update_resource(self);
- }
-
- if time.duration(time.now(), last_retrieved) > max_age {
- update_resource(self);
- }
+Cached_Resource.get :: (use self: &#Self) -> ? self.Res {
+ if !resource {
+ update_resource(self);
+ }
- return self.resource;
+ if time.duration(time.now(), last_retrieved) > max_age {
+ update_resource(self);
}
+
+ return self.resource;
}
#local
-#inject time {
- duration :: (t2, t1: time.Timestamp) -> i32 {
- t1_ := t1;
- t2_ := t2;
- return ~~(t2_->to_epoch() - t1_->to_epoch());
- }
+time.duration :: (t2, t1: time.Timestamp) -> i32 {
+ t1_ := t1;
+ t2_ := t2;
+ return ~~(t2_->to_epoch() - t1_->to_epoch());
}
// Also, free it later by defering io.writer_free. This also flushes
// the internal buffer of io.Writer to make sure everything is written
// to the file.
- file_writer := io.writer_make(&file);
- defer io.writer_free(&file_writer);
+ file_writer := io.Writer.make(&file);
+ defer io.Writer.free(&file_writer);
// The simplest way of writing a string to the file.
- io.write(&file_writer, "This is the first line of text.\n");
+ file_writer->write("This is the first line of text.\n");
// io.write_format can be used to "printf" into a file.
// printf can be thought of as io.write_format(&stdio.stream, ...).
- io.write_format(&file_writer, "This is a {} line of text.\n", "formatted");
+ file_writer->write_format("This is a {} line of text.\n", "formatted");
for i in 0 .. 5 {
- io.write_format(&file_writer, "Another line numbered {}.\n", i);
- }
-}
-
-#doc "Reads example text from a file."
-read_data_from_file :: (filename: str) {
- // This is another way of opening a file. Because of the semantics
- // of `for` loops over Iterators, they can behave like `with`
- // statements in Python or `using` statements in C#.
- for file in os.with_file(filename, .Read) {
- // Create a io.Reader over the file stream.
- file_reader := io.reader_make(file);
- defer io.reader_free(&file_reader);
-
- // Read a single line.
- first_line := io.read_line(&file_reader);
- printf("First line: {}\n", first_line);
-
- // Use io.lines to create an iterator over the remaining
- // lines in the file.
- printf("Remaining lines:\n");
- for line, index in io.lines(&file_reader) {
- printf("{}: {}\n", index, line);
- }
+ file_writer->write_format("Another line numbered {}.\n", i);
}
}
filename := "test.txt";
write_data_into_file(filename);
- read_data_from_file(filename);
read_whole_file(filename);
}</code></pre>
<p class='author'>Brendan Hansen</p>
<div class='description'>This example reads a single line of input from <a href="https://en.wikipedia.org/wiki/Standard_streams">standard input</a>, splits it in half on the first space using <a href="https://docs.onyxlang.io/packages/core.string.html#bisect">string.bisect</a>, converts both parts to integers using <a href="https://docs.onyxlang.io/packages/core.conv.html#parse">conv.parse</a>, then prints the results using <a href="https://docs.onyxlang.io/packages/core.html#printf">printf</a> for formatted printing.</div>
<pre class='hljs'><code class='language-onyx'>// Use the necessary core libraries
-use core.io
-use core.string
+use core.io { Reader }
use core.conv
// Use the printf function that lives in the core package.
// This cannot be `use core.printf`, because that will look
// for a package called `printf` in `core`.
use core {
- printf
+ printf, stdio
}
main :: () {
// Create a io.Reader over the stdio.stream to be able scan
// the input in parts. Also, defer freeing the reader until
// the end of `main`.
- stdin_reader := io.reader_make(&stdio.stream);
- defer io.reader_free(&stdin_reader);
+ stdin_reader := Reader.make(&stdio.stream);
+ defer Reader.free(&stdin_reader);
// Read a single line of input.
- line := io.read_line(&stdin_reader);
+ line := stdin_reader->read_line();
// Split the line on the first space.
- a_str, b_str := string.bisect(line, " ");
+ a_str, b_str := line->bisect(" ");
// Parse and convert both parts to i32s, with a default value
// of 0 if it fails to parse as an i32.