#11108·nushell

A proper type system for Nu

Author: htngrCreated Nov 20, 2023Updated Sep 29, 2026
Labelscategory:enhancementA:type-system

Related problem

We at Codchi are currently considering a rewrite from Haskell to Nushell. Coming from Haskell, however, I miss a few things which are crucial for a typed functional language, especially if you want to write more than just a script. Don't get me wrong, Nushell is already an amazing shell, but there are some enhancements that would enable writing larger applications in Nushell.

Describe the solution you'd like

Algebraic Datatypes

A really useful feature of Rust and Haskell is Algebraic Datatypes, a fancy name for records and sum types. The latter is known as enum in Rust and is used for Result, Option and so on. This is one of the Features which set Rust and Haskell apart from more mainstream languages like Java, Go, C# and so on.

Nushell already has record<...> and table<...>, but lacks a convenient way to define them (see #10630). I think an ergonomic solution is to allow multiline record declarations and standalone types, just like struct in Rust:

nushell
type foo = record<
    bar: int,
    baz: string,
    my_field: my_custom_type,
    my_other_field: my_alias
    ...
>
type my_custom_type = record<...>
type my_alias = decimal

On the other hand, Nushell lacks sum types. The syntax could look like this:

nushell
type my_sumtype = enum<
  variant_a: int,
  variant_b: record<foo: string, bar: int>
>

Also they require pattern matching support to force handling all possible variants of a sum type:

nushell
def handle [x: my_sumtype] {
  match $x {
    variant_a value => { print ($value * 2) }
    variant_b r => { print $"(r.foo): (r.bar)" }
  }
}
> handle (variant_a 21)
42
> handle (variant_b {foo: "Foo", bar: 123 })
Foo: 123

Type checking a Nushell program

Currently (as of 0.87.0) Nushell allows incorrect programs and throws errors only at runtime. This is obviously bad for application development. Consider the following examples:

nushell
> def foo [x: record<bar: int>] { $x.baz } # ❌ no error but obviously incorrect
> foo { baz: 2 } # ✅ correctly throws a type error
Error: nu::parser::type_mismatch
  × Type mismatch.
   ╭─[entry #16:1:1]
 1 │ foo { baz: 2 }
   ·     ─────┬────
   ·          ╰── expected record<bar: int>, found record<baz: int>
   ╰────

> foo { bar: 2 } #  runtime error...
Error: nu::shell::name_not_found
  × Name not found
   ╭─[entry #13:1:1]
 1 │ def foo [x: record<bar: int>] { $x.baz }
   ·                                    ─┬─
   ·                                     ╰── did you mean 'bar'?
   ╰────

Also:

nushell
> let mod10 = {|x: int| $x mod 10 }
> do $mod10 25
5
> let x = do $mod10 "asd" # ❌ no type error...
> $x #  runtime error
Error: nu::shell::type_mismatch
  × Type mismatch during operation.
   ╭─[entry #1:1:1]
 1 │ let mod10 = {|x: int| $x mod 10 }
   ·                       ─┬ ─┬─ ─┬
   ·                        │  │   ╰── int
   ·                        │  ╰── type mismatch for operator
   ·                        ╰── string
   ╰────

I think Nushell should try to push errors from runtime to type checking as much as possible, this is the point of static typing after all. This would also benefit the integrated language server #10794 which could offer better completions and diagnostics and speedup the development as errors are shown directly in the IDE and not at runtime. Type inference would enhance this even more, supporting automated type propagation throughout the program, which would enhance correctness in the whole application overall.

Parametric polymorphism (Generics in Rust)

If you describe $mod10 from the previous example, Nushell prints just closure. There are already generics in Nu (list<int>, record<foo: string>, ...). Why not extend this to closure<a, b> for a closure from a to b ($mod10 would get the type closure<int, int>)? This would preferably be extended to user defined types. One then could write something like:

nushell
type result<a> = enum<
    ok a,
    err string
>
def unwrap<a> []: result<a> -> a {
    match $in {
        ok x => { x }
        err msg => { error make { msg: $msg } }
    }
}

Also this enables typed higher order functions:

nushell
def map<a,b> [f: closure<a,b>]: list<a> -> list<b> { ... }
def compose<a,b,c> [g: closure<b,c>, f: closure<a,b>]: a -> c { ... }

Even better, although probably unrealistic for a shell: Add traits (Rust) / type classes (haskell).

TL;DR

Implement following features from typed functional languages like Haskell to make Nushell ready for application development:

  1. Algebraic Datatypes (Enums / Sum types and standalone type definitions)
  2. Strictly enforce types in a Nushell program (Pushing runtime errors to type errors)
  3. Add generics to closure and allow them in user defined types (see 1.)

Conclusion

All these things are well studied in PL research, which Nushell could imensly benefit from. I think (although I'm not an expert in this field) Nushell could be based on gradual[^1] Hindley–Milner typing. This would also enable full program type inference[^2]. I know this is a lot and probably brings changes to the internals, but as Nushell is approaching 1.0 (which will be backwards-compatible from then on, AFAIK) these are important features which would future-proof Nushell and drive its adoption, especially among FP / Nix people.

[^1]: Gradual typing is a mix of dynamic and static typing. The best known example is Typescript [^2]: Yusuke Miyazaki, Taro Sekiyama, and Atsushi Igarashi. 2019. Dynamic type inference for gradual Hindley–Milner typing. https://doi.org/10.1145/3290331

Describe alternatives you've considered

No response

Additional context and details

No response