Closed player-03 closed 2 years ago
This might be a bit too specific because it's really about any use of the local variable in a way that doesn't allow deconstruction. Not sure how to express that succinctly though.
We can also add that extern inline function
declaration disables deinlining, and compiler starts showing errors in cases, when inline is not possible.
This might be a bit too specific because it's really about any use of the local variable in a way that doesn't allow deconstruction. Not sure how to express that succinctly though.
Off the top of my head, here are some options:
- The local variable must allow deconstruction. (For instance, it must not be passed to a function or returned.)
- The local variable may not be passed to a function, returned, or otherwise used in a way that prevents deconstruction.
Though I've also noticed that you don't actually need to assign to a local variable. Using hxmath's Vector2
class, you can write trace((new Vector2(2, 3) + 5 * new Vector2(-1, -1)).length)
and it will inline both constructors. So the real requirement is that the class instance must be used in a way that allows deconstruction, whether it's assigned to a local variable or not. (And "remaining local" is just one of the criteria.)
Tried another rewrite. Any better?
We can also add that
extern inline function
declaration disables deinlining, and compiler starts showing errors in cases, when inline is not possible.
This is also true if you write inline new MyClass()
, which is described on its own page. Probably should be mentioned here too, but I might make a different pull request for that.
I think that last line raises more questions than it answers... Introducing the term "deconstructible" doesn't help much.
Maybe instead of talking about all the things we can't do, it's easier to talk about what we can do with the local variable, which is accessing its fields.
Better?
If a constructor is declared to be inline, the compiler may try to optimize it away in certain situations. For this to work, the compiler must be able to verify that the object can be replaced by a set of local variables.
- The newly-constructed instance must only be accessible from the local function.
- All called instance functions must be inlined.
- All other references to the instance must only read or write its variables.
Seems good now. Thank you for your patience!
Thank you for your patience!
This only took three days and three revisions. From my perspective that's really fast.
Here's some code demonstrating the limitations.