← Back to context Comment by latch 8 days ago And what pattern would you recommend if you needed to allocate? 2 comments latch Reply srcreigh 8 days ago It's worth every effort to avoid situations where a function creates extra clean up responsibilities to the caller only on error conditions.If I really needed a large error payload, too big to fit on the stack, I'd probably want to do something like this: const errbuf = try alloc.alloc(u8, 1024*1024*1024); module.setErrorBuf(&errbuf) defer { module.setErrorBuf(null); alloc.free(errbuf); } var func_diag = diagnostics.OfFunction(module.func){}; module.func(foo, bar, &func_diag) catch |err| switch (err) { error.BigPayload => { const payload = func_diag.get(error.BigPayload); // The payload can reference the 1MiB of data safely here, // and it's cleaned up automatically. } } dns_snek 8 days ago The diagnostic struct could contain a caller-provided allocator field which the callee can use, and a deinit() function on the diagnostic struct which frees everything.
srcreigh 8 days ago It's worth every effort to avoid situations where a function creates extra clean up responsibilities to the caller only on error conditions.If I really needed a large error payload, too big to fit on the stack, I'd probably want to do something like this: const errbuf = try alloc.alloc(u8, 1024*1024*1024); module.setErrorBuf(&errbuf) defer { module.setErrorBuf(null); alloc.free(errbuf); } var func_diag = diagnostics.OfFunction(module.func){}; module.func(foo, bar, &func_diag) catch |err| switch (err) { error.BigPayload => { const payload = func_diag.get(error.BigPayload); // The payload can reference the 1MiB of data safely here, // and it's cleaned up automatically. } }
dns_snek 8 days ago The diagnostic struct could contain a caller-provided allocator field which the callee can use, and a deinit() function on the diagnostic struct which frees everything.
It's worth every effort to avoid situations where a function creates extra clean up responsibilities to the caller only on error conditions.
If I really needed a large error payload, too big to fit on the stack, I'd probably want to do something like this:
The diagnostic struct could contain a caller-provided allocator field which the callee can use, and a deinit() function on the diagnostic struct which frees everything.