Nim: Desctructors/sinks/assigments not injected in top-level scope

Created on 19 Sep 2018  路  5Comments  路  Source: nim-lang/Nim

Sink and assignment operations are never called when appearing as top-level statements.
The transformation is only done for function body nodes (see here).

Uncommenting these lines fixes this.

I suppose there is a reason why this is disabled. Would re-enabling it, and checking for global scope, have the expected behavior?

Destructors

Most helpful comment

This has been fixed.

All 5 comments

I suppose there is a reason why this is disabled. Would re-enabling it, and checking for global scope, have the expected behavior?

Obviously not. :-) Problem is we need a "top level statements collector" in order to support this properly and that requires a (minor?) compiler refactoring.

we worked this around for now with cpp shared pointers: https://github.com/fragcolor-xyz/fragments/commit/9982e45278edf91961cabda4cf59cab4c9d86871

Would be nice to have a fix tho but I suspect it's related to the remaining work on destructors/gcdestructors I guess, so better do things at the right pace.

Er, isn't the obvious workaround to put your code in a main proc? Top level statements are rarely required, they are mostly a convenience.

Generally speaking obviously yes.

But we are trying to drag people from python to nim with our project so I completely see users writing stuff in the global scope.

Those variables will hold tensors that possibly are owning GPU memory and so GC refs ( which are unaware of the actual weight in memory) are not viable. Using objects is our only best clean solution.

We will migrate to gc destructors possibly tho finally, so we could just have a special allocator for tensors.

This has been fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

teroz picture teroz  路  3Comments

koki-koba picture koki-koba  路  3Comments

Tronic picture Tronic  路  3Comments

kobi2187 picture kobi2187  路  4Comments

capocasa picture capocasa  路  3Comments