Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Invariant for this type is: key_1 == key_2
- // no function here breaks it
- struct Bar{
- private{
- field other:shared_ptr[Bar];
- field key_1:i32;
- field key_2:i32;
- }
- function new(k:i32):Bar{
- return Bar{key_1 = k, key_2 = k};
- }
- function set_other(self:ref Bar, other:shared_ptr[Bar]){
- self->other = other;
- }
- function foo(bar:ref Bar){
- print(bar->key_1, bar->key_2);
- }
- function copy(target:out Bar, source:ref Bar){
- target->key_1 = source->key_1;
- if ( not source->other.empty() ){
- let other = source->other.get(); //ref Bar
- print(other->key_1, other->key_2); // prints 2 1
- }
- target->key_2 = source->key_2;
- }
- };
- function foo(){
- var a:shared_ptr[Bar] = shared_ptr::new(Bar::new(1));
- var b:shared_ptr[Bar] = shared_ptr::new(Bar::new(2));
- // copies
- external_call(a, b);
- *a = *b;
- }
- // compiler does not see the source code for this function
- function external_call(a b:shared_ptr[Bar]){
- b->set_other(a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement