Browse the documentation

Design decisions

The decisions that shape Typhon: C# as an intermediate language, a source generator, static types by default, valid Python syntax, names as written and bridged protocols.

This page records the main design decisions of the proof of concept and their costs. They describe the current state. The project has not fixed any of them for the future.

C# as intermediate language

Typhon generates C# and lets Roslyn produce the IL. Emitting IL directly would mean reimplementing C#‘s overload resolution, generic inference, lambdas and closures, the state machines for async and iterators, dynamic call sites and debug symbol generation. Through Roslyn, Typhon gets all of these, and the IL comes from the C# compiler itself. The front end does not depend on the back end, so a direct IL back end remains possible.

A source generator in place of a separate compiler

The compiler runs as a Roslyn source generator. It plugs into any C# project without changes to the .NET SDK, it works in the IDE, and it lets C# and Typhon reference each other inside one assembly. The approach has two costs. A project with only .ty files is still a .csproj, and the generator runs on every keystroke in the IDE, so it has to stay fast. A dedicated MSBuild SDK (<Project Sdk="Typhon.Sdk">) could follow.

Static types by default, dynamic as an escape hatch

Speed and C# interoperability both depend on static types. Gradual typing with whole-program inference keeps porting existing Python possible, and dynamic stays available where no static type exists. typhon build --aot additionally rejects dynamic values that come from libraries (TY2001), because Native AOT cannot run late-bound code.

Valid Python as a constraint

Typhon adds no syntax of its own, so a .ty file is source that CPython can parse. Editors, formatters and linters for Python therefore work on .ty files. The same file can also run under both runtimes, which makes the cheapest conformance test possible: run the file with CPython and with Typhon and compare the outputs. The conformance tests compare outputs this way.

Names as written

calculate_area stays calculate_area for C# callers. A leading _ makes a member internal or protected internal, and a leading __ makes it private. An option that maps names to PascalCase would be easy to add if C# consumers ask for it.

Bridged Python protocols

Typhon connects Python’s protocols to their .NET counterparts. __eq__ becomes Equals and ==, __iter__ becomes IEnumerable<T>, and __getitem__ becomes an indexer. Typhon objects therefore behave like native objects in C#, in LINQ and in data binding.