cxgo icon indicating copy to clipboard operation
cxgo copied to clipboard

Support static specifier

Open dennwc opened this issue 4 years ago • 2 comments

Currently static specifier is ignored, leading to incorrect code (variable is defined in the local scope, with the lifetime of a block).

This is a serious issue and it must be addressed.

dennwc avatar Jan 10 '21 19:01 dennwc

static variables should be able to be implemented with closures. For example: int fun() { static int count = 0; count++; return count; } can be implemented as:

var fun = _Static_fun()

func _Static_fun() func() int {
	var count int
	return func() int {
		count++
		return count
	}
}

This should be easy to implement because all that must be done is move all static variables to the top and create the closure with the rest of the code.

TotallyGamerJet avatar Jul 13 '21 01:07 TotallyGamerJet

Indeed, that could be a way, but closures proved to be problematic, since the expression might appear either as lhs or rhs, so you will need to return pointer there. And the code gets far less readable which I'd like to avoid as much as I can. So the plan was to allocate unique names for static, and just keep them in the global scope.

dennwc avatar Jul 13 '21 09:07 dennwc