--- tags: ytt,research --- # Scratchpad Notes: Hoisting Functions from YAML Templates https://github.com/vmware-tanzu/carvel-ytt/issues/526 Cases: - Case: Simplest - Case: Guilt by Association - Case: Ordinary Functions (i.e. not YAML Fragments functions) ## Case: Simplest ```yaml= ``` ## Case: Guilt by Association ### Annotation Guilty by Association ```yaml= a: b: 1 #@overlay/replace via=lambda l,r: 42 #@ def foo(): b: 2 #@ end #@overlay/match by=lambda i,l,r: True --- a: #@ foo() ``` ... without hoisting results in: ```yaml a: b: 42 ``` ... but the `@overlay/replace` is semantically _not_ part of `foo()`. Hoisting would result in: ```yaml= #@ def foo(): b: 2 #@ end --- a: b: 1 #@overlay/replace via=lambda l,r: 42 #@overlay/match by=lambda i,l,r: True --- a: #@ foo() ``` ... which results in: ```yaml 42 ``` ### Code Guilty by Association ```yaml= #@ x = 13 a: b: 1 #@ x = 42 #@ def foo(): b: #@ x #@ end #@overlay/match by=lambda i,l,r: True --- a: #@ foo() ``` ## Case: Non-local variables within a function ```yaml= a: b: 1 #@ x = 42 #@ def foo(): b: #@ x #@ end #@overlay/match by=lambda i,l,r: True --- #@ x = 13 a: #@ foo() ``` ```yaml= --- #@ def foo(): b: #@ x #@ end --- a: b: 1 #@ x = 42 #@overlay/match by=lambda i,l,r: True --- a: #@ foo() #@ x = 13 ```