Working on the editor - the g/fnk macro

Can someone explain what is this fnk clojure macro in ns dynamo.graph? It’s defined in dynamo/graph.clj. It’s used all over the place but its definition is kind of cryptic.

I’ve come across this on a sound issue i’m working on and seems i need to sort this out to properly handle default values on a property and validation.

Thanks

1 Like

The g/fnk macro is used in node definitions to define the input dependencies and calculate the resulting value of an output. Each name in the argument list corresponds to an input, output or property on the node the g/fnk is used in. When the output that hosts the g/fnk is evaluated, each of the names inputs are evaluated and then fed as arguments into the g/fnk. You could view it as a regular fn with added metadata about its dependencies.

2 Likes

If you don’t want to inline the g/fnk in the node definition, you could use g/defnk to create a named fnk outside the node and refer to it by name instead.

Thanks mats.ggisselson for you clean explanation.

I see cases where an input dependency of a property is the property itself. Is this typical ?

Yes, that is normal. If a g/fnk is used for the value form of a property declaration, the input to the g/fnk will be the “raw” value of the property, and whatever the g/fnk returns will be the value of the property that the outside world sees.

This is used a lot for situations like where a property was assigned a resource path, but then the resource was moved on disk. You want the outside world to see the new path to the moved resource.