Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Hello World **/
- // Sources in this file will be added to the examples unit.
- module examples;
- // Searches for symbols in all std modules.
- import std.*;
- // Program entry point.
- main:: (args: std.array<std.string>)[exit: int] {
- // Print Hello World! and exists with code 0 (success).
- std.println("Hello World!");
- exit = 0;
- }
- /** Structs **/
- module examples;
- import std.*;
- // A struct with no inheritance.
- Animal:: [
- // A struct that extends a reflection type, enums are not a Hash feature,
- // the AST is passed to a compile-time method to modify the extending struct.
- Type:: std.enum<uint32> [
- Dog; // 1
- Cat; // 2
- Ferret = 5; // 5
- Fish; // 6
- ]
- // Members to the Animal struct.
- type: Type;
- name: std.string;
- age: uint32;
- // A method that prints Animal struct data.
- print:: ()[] {
- std.println("Type: %, Name: %, Age: %", type, name, age);
- }
- ]
- main:: (args: std.array<std.string>)[exit: int] {
- // A designated initializer list to create an Animal.
- jack := Animal[
- .type = Type.Dog;
- .name = "Jack";
- .age = 7;
- ];
- // A "constructor"-like list to create an Animal.
- // Hash has no constructors or destructors!
- lily: Animal = [Type.Cat, "Lily", 3];
- // Calls a methods from a stack object.
- jack.print();
- lily.print();
- }
- /** Compile-time Reflection **/
- module examples;
- import std.*;
- interface## std.reflect [
- // Reflection works as a compile-time system for modifying+generiting methods and members into a extender type.
- // Any methods defined as runtime (::) will be copied into the extending struct.
- // Private methods defined in compile-time (##) can only be used from inside this body.
- // Compile-time structs can extend from other compile-time structs, but not runtime, runtime can extend both kinds.
- // Compile-time methods are only applied to the extender, it will not change parent or child structs inthe inheritance tree.
- // Any variables modified in compile-time must have been passed into a compile-time method, or defined as compile-time types (`static count#: int;`), they are not avalable during runtime.
- override process## (unit: *std.reflect.struct)[] {
- std.assert(source.variables.empty(), "Interfaces must not contain data members");
- foreach (method; source.methods) {
- validate_method(method);
- }
- }
- private validate_method## (method: *std.reflect.method)[] {
- if (method.is_static) return;
- std.assert(!method.is_public, "Interfaces methods must be public");
- std.assert(!method.has_body, "Interfaces methods must be virtual");
- }
- ]
- A:: interface [
- // Must not have any function bodys, or members. Static functions are ok!
- method:: (a: int, b: float)[ret: bool];
- ]
- Impl: A [
- // TODO: A.method instead of override? Would remove need for virtual inheritance.
- override method:: (a: int, b: float)[ret: bool] {
- return (2 * a) > b;
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement