Files
c3c/resources/examples/contextfree/cleanup.c3
Christoffer Lerno 5c77c9a754 - Change distinct -> typedef.
- Order of attribute declaration is changed for `alias`.
- Added `LANGUAGE_DEV_VERSION` env constant.
- Rename `anyfault` -> `fault`.
- Changed `fault` -> `faultdef`.
- Added `attrdef` instead of `alias` for attribute aliases.
2025-03-15 20:10:47 +01:00

50 lines
961 B
Plaintext

import std::io;
struct Resource
{
String name;
}
faultdef WELCOME_TO_YOUR_DOOM;
fn Resource? resource_init(String name)
{
io::printfn("open %s", name);
return { name };
}
fn void Resource.deinit(Resource this) => io::printfn("close %s", this.name);
macro void? @open_with(String name; @body(Resource resource))
{
Resource resource = resource_init(name)!;
defer
{
io::printn("Using open_with to close");
resource.deinit();
}
@body(resource);
}
fn Resource? prep_out(String out_name, String[] prep_names)
{
Resource writer = resource_init(out_name)!; // Rethrow the optional result
defer catch writer.deinit();
foreach (name : prep_names)
{
@open_with(name; Resource reader)
{
io::printfn("use %s", reader.name);
// if (true) return WELCOME_TO_YOUR_DOOM?;
}!;
}
return writer;
}
fn void main()
{
Resource writer = prep_out("out", (String[]) { "a", "b"})!!;
defer writer.deinit();
io::printn("use out");
}