Skip to content
News

· Ant 0.6.0

Packages, and JSON read by type

Programs import packages from git repositories by name, and Json.decode reads JSON straight into records of a named type.

Ant 0.6 is about code and data that come from outside a program. Until now a program could use the files it imported and the prelude, and nothing else. JSON reached a program as a tree of Json.Value, and each program turned it into its own types by hand. Now a program can import packages, and read JSON into its types in one call.

Packages ​

The spec said it plainly: "There are no packages: a program is the files it imports." Code was shared by copying files, and a copy has no version, no source, and no way to take a fix. Whatever many programs needed had to become part of the prelude, which ships in every binary. And a script, a single file with a #! line, could say nothing about what it needed beyond the prelude.

A package is now a git repository of Ant files with an ant.json at its root. Its author publishes a version by pushing a tag. There is no registry and no account. ant add writes the project's ant.json, fetches the package with git, and pins it in ant.lock. The examples here use example.com/slug, the small package from the packages guide, which turns a title into the part of an address that names a page:

sh
ant add example.com/slug
txt
ant: fetching example.com/slug 2.0.0
+ example.com/slug 2.0.0

Every file of the project then imports the package by its name:

ant
import "slug" Slug

println("/posts/${Slug.make("Crème brûlée")}")
txt
/posts/crème-brûlée

ant.lock holds one line per package, its version and a hash of its files, so every machine runs the same code. The version in ant.json is a minimum, and Ant takes the highest minimum anyone asks for, never the newest release, as Go modules do. A new release reaches a project only through ant update or ant add.

A script with no ant.json writes the version in the import. The first run fetches the package, later runs read it from the store, and the file stays complete on its own:

ant
#!/usr/bin/env ant
import "example.com/slug@1.2.0" Slug

println(Slug.make("Hello, Ant"))
txt
ant: fetching example.com/slug 1.2.0
hello-ant

Fetching a package runs none of its code. A package's code runs only when the program runs, and it can do then what the prelude lets any code do. Ant asks for no credentials and keeps none: a private repository is fetched with whatever git on the machine already uses.

Typed JSON ​

Reading JSON into records took a decoder function per type, about ten lines for a record of two fields, or a walk over the type with Reflect that each program copied and then owned. The walk was slow: on a 10 MB file of records it took 1171 ms, where Json.parse alone took 98 ms. Json.parse also reads every number as a float64, so an id above 2^53 lost a digit before any code saw it. And Json.stringify writes a record without its tag, so a value of an enumeration such as Level below came out as {}, which nothing can read back.

Json.decode reads text into a value of the type it is given, with each record's tag and defaults, and says where the text does not fit. Json.encode writes a value as its type describes it, so that Json.decode reads it back:

ant
type Debug = {}
type Info = {}
type Level = Debug | Info

type Server = { host: string, port: int = 8080 }
type Config = { name: string, level: Level = Info{}, servers: Array[Server] }

let text = `{
  "name": "prod",
  "level": "Debug",
  "servers": [{"host": "a.example"}, {"host": "b.example", "port": 9000}]
}`

let config = try Json.decode(text, type(Config))
println(config.servers[0])
println(config.level is Debug)

let bad = `{"name": "prod", "servers": [{"host": "a", "port": "9000"}]}`
println(Json.decode(bad, type(Config)))

println(Json.encode(type(Config), config))
txt
Server{ host: "a.example", port: 8080 }
true
Err{ error: Error{ message: "$.servers[0].port: want int, got \"9000\" at line 1, column 52", kind: null } }
{"name":"prod","level":"Debug","servers":[{"host":"a.example","port":8080},{"host":"b.example","port":9000}]}

Nothing is converted to make the text fit: "9000" is not an int. An int field keeps every digit, so 9007199254740993 stays 9007199254740993. A member the type has no field for is skipped, or is an error with rejectUnknown: true. Json.Raw keeps a part of the text as written, to decode once another part has said what it is.

Json.decode builds the records and no map per object, so it is faster than Json.parse: on the same 10 MB of records it takes 38 ms to Json.parse's 104 ms, about what Go's encoding/json/v2 takes to read the file into Go structs. A type with no JSON form, such as a function type or a union of two members that read the same kind of JSON value, makes it panic, whatever the text.

Also new ​

  • is tests a named record against an anonymous record member of a union, and a type parameter through its bound. A union bound takes a type that fits one of its members.
  • ant help <command> says what a command does and lists its flags.
  • Unchecked code: under ant run, an assignment to a field of a prelude namespace, such as String.trim = f, panics, as an assignment to println already did. It used to replace the built-in for the rest of the process. The checker already rejected it.

Breaking changes ​

  • An import of a file of the program starts with ./ or ../. Write import "./util.ant", not import "util.ant": a path that starts with a name now names a package. The error offers the fix.
  • Empty square brackets after a type or a declaration name, such as int[] or func f[](), are a syntax error. Write Array[int], and leave out the brackets of a declaration with no type parameters.
  • is on a generic type whose union holds more than one type parameter checks only when every type parameter agrees. A tag carries no type arguments, so the old rule let a program read a value at the wrong type.

See the full changelog.