GoPatina

Interfaces and errors as values

By Peter Sjolin, .

GoPatina compiles a whole program at once, so it knows every type that is ever stored in an interface. That made a simple design possible for RFC 0012: an interface value is one Rust enum of those types, and a method call through an interface is a match on it. There are no trait objects, no Any and no unsafe, and a program that uses no interface pays nothing.

Interfaces

Interfaces are supported with conversions, method calls, comparisons, type assertions, type switches and printing, and with Go's exact panics: a method call on a nil interface, a failed assertion, a missing method, and comparing values of a type that cannot be compared.

Errors as values

error is now an ordinary interface value: kept, stored, compared, printed and switched on, while functions still return Rust's Result. The errors API follows Go's errors package: fmt.Errorf with %w, errors.Unwrap, errors.Is and errors.As. Sentinel errors such as var ErrNotFound = errors.New("not found") work across packages.

Where translation stops

RFC 0011 draws the line between the standard library GoPatina translates from source and the packages it provides in Rust with Go's behaviour. The first provided APIs are the common strings functions, whose case mapping uses Go's own Unicode tables (Rust's differ), checked against Go on every code point, and time.Duration.

Defects found on the way

Writing the tests for these features turned up defects, and each was fixed in its own pull request with golden programs and generator cases: a false rejection by the borrow checker (#80), calls of another package's error functions that did not build (#82), and the translation of time from source (#76). The correctness page lists every defect found so far.

Drafted with AI assistance; every figure links to its evidence.