RSS Amplifier

Bit Maybe Wise · Apr 23, 2026

Tsonnet #40 - Call me maybe, but make it typed, part 6

0
Sign in to vote or save

Hercules Merscher · Bit Maybe Wise

Welcome to the Tsonnet series!

If you’re not following along, check out how it all started in the first post of the series.

In the previous post, we implemented methods:

After all this work implementing functions and methods, there’s still one thing missing: function calls with named parameters. I implemented the definition, but not the call site when named parameters are present.

// samples/functions/named_params.jsonnet
local my_function(x, y=10) = x + y;
my_function(2, y=3)

This calls for a new type variant: call_arg.

This way we can leverage the expressiveness of the OCaml type system to enforce a Positional or Named argument.

The parser needs a little tweaking:

With the addition of named parameters to function calls, they need to be translated by the type checker:

I feel like translate_named_function_call and translate_closure_call could be unified, similar to what I did with apply_function in the interpreter. They differ in subtle ways though, so I’ll leave that for another day.

The interpreter changes are only following the new type variant addition:

With that I can put a closure (pun intended) to the function tutorial.

The standard library has to wait. We must implement other building blocks, such as conditionals, for loops, etc — the usual stuff we can’t live without in a programming language.

Have you tried Pisco Sour already? If you’re into alcoholic drinks, give it a try. It has a tasty flavour.

Ta-da! Cram tests in place:

And that’s a wrap on Tsonnet’s function support (for now). Named parameters, default values, closures, methods — it’s all there. The type checker knows how to count positional arguments, match them against names, and fill in defaults. Not bad for a type system that started life unable to tell a function from a hole in the ground.

This closes the chapter on functions in the Jsonnet tutorial too. We went from bare locals all the way to my_function(y=3, x=2) working correctly, with a Pisco Sour to show for it.

Here is the entire diff.

Read the original on bitmaybewise.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.