You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With 0b6cf37, I introduced a very primitive ownership implementation into the Semantic Analysis pass. The biggest caveat of this is that it will error when moving primitive types, such as ints.
In Rust, you can't use a struct once you've moved it.
structP{x:i32,}fnmain(){let a = P{x:7};let b = a;let c = a.x + b.x;println!("{}", c);}
This code will result in the following error:
rustc 1.16.0(30cf806ef 2017-03-10)
error[E0382]:use of moved value: `a.x`
--> <anon>:8:13
|
7 | let b = a;
| - value moved here
8 | let c = a.x + b.x;
| ^^^ value used here after move
|
= note:move occurs because `a` has type `P`, which does not implement the `Copy` trait
error: aborting due to previous error
This behavior has been implemented:
class P
let x : Int
init (v : Int)
x = v
;
main () : Int
let a = new P(7),
let b = a,
let c = a.x + b.x,
0
11 let b = a,
12 let c = a.x + b.x,
~~~~~~~~~~~~^
13 0
Reference to 'a' has already been used.
However, Rust handles primitives differently...
fnmain(){let a = 7;let b = a;let c = a + b;println!("{}", c);}
This code is perfectly allowed in Rust, since a is being copied to b when it is moved.
main ()
let a = 7,
let b = a,
let c = a + b,
0
This will currently error with the following:
4 let b = a,
5 let c = a + b,
~~~~~~~~~~~~^
6 0
Reference to 'a' has already been used.
If we look at the above Rust error, we can see that our struct didn't implement the Copy trait. Integers in Rust implement this trait, and thus can be copied. It would be easy to create a special case for our defined primitive types, but having something like "traits" would be beneficial to users who want their classes to be copyable. Maybe an annotation?
The text was updated successfully, but these errors were encountered:
I think this could be a good idea. I've always liked the idea of annotations, but making the language annotation heavy might not make it super intuative.
I think classes should be value-based by default and should have to explicitly request reference schematics (forcing heap-allocation). If this is done, the compiler is free to stack-allocate and copy immutable objects, allowing boxing to be deferred indefinitely.
With 0b6cf37, I introduced a very primitive ownership implementation into the Semantic Analysis pass. The biggest caveat of this is that it will error when moving primitive types, such as
int
s.In Rust, you can't use a struct once you've moved it.
This code will result in the following error:
This behavior has been implemented:
However, Rust handles primitives differently...
This code is perfectly allowed in Rust, since
a
is being copied tob
when it is moved.This will currently error with the following:
If we look at the above Rust error, we can see that our struct didn't implement the
Copy
trait. Integers in Rust implement this trait, and thus can be copied. It would be easy to create a special case for our defined primitive types, but having something like "traits" would be beneficial to users who want their classes to be copyable. Maybe an annotation?The text was updated successfully, but these errors were encountered: