qupa-project / uniview-lang

View once immutability enabling the safeties of immutable code, while enjoying near procedural performance
https://uniview.qupa.org
MIT License
2 stars 0 forks source link

Lending/Cloning Values #19

Closed AjaniBilby closed 3 years ago

AjaniBilby commented 3 years ago
AjaniBilby commented 3 years ago

Test Case

import "print.uv";

struct Person {
    id: int;
    age: float;
}

fn init(): Person {
    let p = Blank#[Person]();
    p.age = 21.0;
    p.id = 0;

    return p;
}

fn age(pp: @Person) {
    pp.age = pp.age + 1.0;
    let b = true;
    if (b) {
        pp.id = 23;
    }

    return;
}

fn print(p: Person) {
    print("Person { id: ");
    print(p.id);
    print(", age: ");
    print(p.age);
    println(" }");

    return;
}

fn main(): int {
    let p = init();
    print( $p );      // Person { id: 0, age: 21 }
    let dup = $p;
    age(@dup);
    print( dup );     // Person { id: 23, age: 22 }
    print( p );       // Person { id: 0, age: 21 }

    return 0;
}