Skip to content

.NET Interop

Amethyst compiles to .NET IL and interoperates bidirectionally with C# and other .NET languages.

All .NET namespaces are available by their full name without any import:

list = System::Collections::Generic::List[String].new
use System::Collections::Generic
list = List[String].new
dict = Dictionary[String, Int].new

Assigning a namespace or type to a PascalCase name declares an alias. Aliases are resolved at compile time and are not constants or values:

Json = System::Text::Json
Names = Array[String]
serializer = Json::JsonSerializer.new
names = Names.new

C# classes, methods, and generics are usable directly. The interop API is intentionally explicit — no hidden rewrites or magic, so code is understandable without IDE tooling.

.NET follows consistent PascalCase conventions, so Amethyst transparently maps between snake_case and PascalCase in both directions.

Calling existing assemblies — lookup order when resolving a name:

  1. Exact match (e.g. add → looks for add)
  2. snake_case → PascalCase conversion (e.g. add → looks for Add)

If a collision exists (both add and Add are defined), the exact-cased variant wins:

list.add("hello") # resolves to List.Add — transparent
list.Add("hello") # resolves to List.Add — exact match

Amethyst output — methods and properties are mangled to PascalCase in the compiled IL. The compiler automatically adds @[AmethystName("original_name")] to preserve the original name for Amethyst consumers:

def save_user(user: User): Void # compiles as SaveUser() in IL
# @[AmethystName("save_user")] added automatically

C# sees SaveUser(). Amethyst code calling the same assembly resolves save_user back to SaveUser via the @[AmethystName] attribute.

C# generic types use Amethyst’s [T] bracket syntax:

list = List[String].new
dict = Dictionary[String, Int].new

Pass out variable at the call site. The variable comes into scope after the call with its type inferred from the C# signature:

success = Int32.try_parse("42", out result)
# result: Int is now in scope

Pass ref variable at the call site:

swap(ref a, ref b)

Amethyst’s own method signatures do not use out or ref — these keywords appear only when calling C# methods that declare them.


Amethyst types compile to standard .NET types and are fully visible to C#:

Amethyst C# sees
class Foo public class Foo
property name: String public string Name { get; set; }
getter name: String public string Name { get; }
def valid? public bool IsValid()
def save! public void Save()
@[Serializable] [Serializable]
namespace MyApp namespace MyApp

Amethyst supports async/await as first-class keywords, mapping directly to .NET’s Task<T> model.

async def fetch(url: String): String
response = await http.get(url)
await response.read_body
end

The return type annotation is written as the unwrapped type. The compiler wraps it in Task<T> in the compiled IL:

Amethyst signature Compiled IL
async def foo: String async Task<string> Foo()
async def foo: Void async Task Foo()
  • await is only valid inside an async method — a compile error otherwise
  • Calling a C# Task<T> method without await returns the Task[T] object
  • await unwraps any Task[T] or Task — including those returned from C# methods
# Calling a C# async method
async def load_body(url: String): String
task = http.GetAsync(url) # Task[HttpResponseMessage] — not yet awaited
response = await task # unwrap
await response.Content.ReadAsStringAsync
end
fetcher = async ->(url: String): String { await http.get(url) }

When calling C# code that predates nullable reference type annotations, use null_safety off at the top of the interop file:

null_safety off
namespace MyApp::LegacyInterop

See Types for details.