moleike / haskell-jsonnet

🎏 Haskell implementation of Jsonnet
https://hackage.haskell.org/package/jsonnet
Other
21 stars 6 forks source link

Gradual types? #14

Open moleike opened 3 years ago

moleike commented 3 years ago

Jsonnet (the spec) defines the language as dynamically typed. An extension that seems natural to incorporate into Jsonnet is gradual typing with structural subtyping à la TypeScript.

Gradual typing enables mixing typed and untyped code, where users decide where (or when) to add type annotations to increase static checking. Fully annotated programs should be statically type-safe. Programs with no annotations at all should behave as current Jsonnet.

In a first (simpler) approach, we erase type annotations after type checking and interpret the program as if it were dynamically typed. A benefit of this approach is the typechecker becomes a standalone component, and thus it can be used with other compilers. There are some quirks though:

local foo(x) = 
  local bar(y: int) = {};
  bar(x);
foo(true)

The above program should intuitively fail, but it runs to completion.

A more elaborate approach performs run-time type checks at dynamically and statically typed code boundaries, by adding explicit casts. In this case, the type checking could be done after desugaring.

Jsonnet original implementation has a related open issue: https://github.com/google/jsonnet/issues/605