.NET Interop
Amethyst compiles to .NET IL and interoperates bidirectionally with C# and other .NET languages.
Namespaces and Imports
Section titled “Namespaces and Imports”All .NET namespaces are available by their full name without any import:
list = System::Collections::Generic::List[String].newuse — bring names into scope
Section titled “use — bring names into scope”use System::Collections::Generic
list = List[String].newdict = Dictionary[String, Int].newAliasing
Section titled “Aliasing”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::JsonNames = Array[String]
serializer = Json::JsonSerializer.newnames = Names.newCalling C# from Amethyst
Section titled “Calling C# from Amethyst”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.
Method and Property Casing
Section titled “Method and Property Casing”.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:
- Exact match (e.g.
add→ looks foradd) snake_case→PascalCaseconversion (e.g.add→ looks forAdd)
If a collision exists (both add and Add are defined), the exact-cased variant wins:
list.add("hello") # resolves to List.Add — transparentlist.Add("hello") # resolves to List.Add — exact matchAmethyst 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 automaticallyC# sees SaveUser(). Amethyst code calling the same assembly resolves save_user
back to SaveUser via the @[AmethystName] attribute.
Generics
Section titled “Generics”C# generic types use Amethyst’s [T] bracket syntax:
list = List[String].newdict = Dictionary[String, Int].newout Parameters
Section titled “out Parameters”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 scoperef Parameters
Section titled “ref Parameters”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.
Exposing Amethyst to C#
Section titled “Exposing Amethyst to C#”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 |
Async / Await
Section titled “Async / Await”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_bodyendThe 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() |
awaitis only valid inside anasyncmethod — a compile error otherwise- Calling a C#
Task<T>method withoutawaitreturns theTask[T]object awaitunwraps anyTask[T]orTask— including those returned from C# methods
# Calling a C# async methodasync def load_body(url: String): String task = http.GetAsync(url) # Task[HttpResponseMessage] — not yet awaited response = await task # unwrap await response.Content.ReadAsStringAsyncendAsync Lambdas
Section titled “Async Lambdas”fetcher = async ->(url: String): String { await http.get(url) }Null Safety at Interop Boundaries
Section titled “Null Safety at Interop Boundaries”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::LegacyInteropSee Types for details.