Skip to content

Structs

You can define custom data types using the type keyword.

// Type definition with default values
type Person {
str name;
int age;
str city = "İstanbul";
}

You can create instances of your struct using a constructor-like syntax.

// Constructor with named arguments
Person p1 = Person("Ali", 25, "Ankara");
Person p2 = Person(name: "Ayşe", age: 30); // Uses default city

Fields can be accessed and modified using dot notation.

// Access and modify
print(p1.name, p1.age);
p1.city = "İzmir";

Structs can be converted to and from JSON.

// Convert struct to JSON string
str jsonStr = toJson(p1);
// Create struct from JSON string
Person p3 = fromJson("Person", jsonStr);