AutolangDocs

Generics

Generics allow you to write reusable and type-safe code that can work across different data types. In Autolang, you can define generic classes and generic functions.

Generic Functions

You can define standalone generic functions using angle brackets <T>. These functions can be resolved by the compiler dynamically based on the passed argument types, or you can supply the type explicitly.

func printItem<T>(item: T) { println(item) } // You must pass the type explicitly: printItem<String>("Hello world") printItem<Int>(123) printItem<Bool>(true)

⚠️ Current Limitations

The compiler currently does not support generic functions inside classes (methods with their own generic parameters). Standard standalone generic functions and generic classes are fully supported.

Generic Classes

Generic classes allow you to parameterize your data structures over types. You can use extends syntax for type bounds, ensuring the generic type follows specific traits.

class GAnimal { func sound() = "Nothing" } class GCat extends GAnimal { constructor() { super() } @override func sound() = "Meow" } // Here we restrict T to extend GAnimal class Box<T extends GAnimal>(var value: T) { func getSound(): String = value.sound() } val myCat = GCat() val box = Box<GCat>(myCat) println(box.getSound()) // Prints "Meow"