Last active
June 2, 2025 13:58
-
-
Save catdevnull/581edc49287edac0a14bac5b5d25b548 to your computer and use it in GitHub Desktop.
quirk: explicit resource management doesn't work like defer in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# explicit resource management: unexpected recursive behavior\n", | |
"\n", | |
"in go, a defer that is run inside a child function runs before the parent defer. javascript's [explicit resource management](https://tc39.es/proposal-explicit-resource-management/) doesn't work like this." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a start\n", | |
"b start\n", | |
"a end\n", | |
"b end\n" | |
] | |
} | |
], | |
"source": [ | |
"function a() {\n", | |
"\tconsole.log(\"a start\")\n", | |
"\treturn {\n", | |
"\t\t[Symbol.dispose]: () => {\n", | |
"\t\t\tconsole.log(\"a end\")\n", | |
"\t\t}\n", | |
"\t}\n", | |
"}\n", | |
"\n", | |
"function b() {\n", | |
"\tusing A = a()\n", | |
"\tconsole.log(\"b start\")\n", | |
"\treturn {\n", | |
"\t\t[Symbol.dispose]: () => {\n", | |
"\t\t\tconsole.log(\"b end\")\n", | |
"\t\t}\n", | |
"\t}\n", | |
"}\n", | |
"\n", | |
"using B = b()\n", | |
"\n", | |
"/* output:\n", | |
"a start\n", | |
"b start\n", | |
"a end\n", | |
"b end\n", | |
"*/" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"a workaround is to not use `using` in the parent function, and manually call the dispose function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a start\n", | |
"b start\n", | |
"b end\n", | |
"a end\n" | |
] | |
} | |
], | |
"source": [ | |
"function a() {\n", | |
"\tconsole.log(\"a start\")\n", | |
"\treturn {\n", | |
"\t\t[Symbol.dispose]: () => {\n", | |
"\t\t\tconsole.log(\"a end\")\n", | |
"\t\t}\n", | |
"\t}\n", | |
"}\n", | |
"\n", | |
"function b() {\n", | |
"\tconst A = a()\n", | |
"\tconsole.log(\"b start\")\n", | |
"\treturn {\n", | |
"\t\t[Symbol.dispose]: () => {\n", | |
"\t\t\tconsole.log(\"b end\")\n", | |
"\t\t\tA[Symbol.dispose]()\n", | |
"\t\t}\n", | |
"\t}\n", | |
"}\n", | |
"\n", | |
"using B = b()\n", | |
"/* output:\n", | |
"a start\n", | |
"b start\n", | |
"b end\n", | |
"a end\n", | |
"*/" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Deno", | |
"language": "typescript", | |
"name": "deno" | |
}, | |
"language_info": { | |
"codemirror_mode": "typescript", | |
"file_extension": ".ts", | |
"mimetype": "text/x.typescript", | |
"name": "typescript", | |
"nbconvert_exporter": "script", | |
"pygments_lexer": "typescript", | |
"version": "5.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment