Skip to content

Gradual types?ย #14

@moleike

Description

@moleike

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: google/jsonnet#605

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions