Skip to content

Instantly share code, notes, and snippets.

@radgeRayden
Last active January 25, 2021 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radgeRayden/67b654b5bb8f3227749b5dd7a577ec4d to your computer and use it in GitHub Desktop.
Save radgeRayden/67b654b5bb8f3227749b5dd7a577ec4d to your computer and use it in GitHub Desktop.
using import struct
# You generally want to use plain structs either to interface with C, or
# to store some pure data (commonly called POD object) that doesn't need any management
struct A plain
a : i32
struct B
b : i32
inline __drop (self)
print "dropping B" self.b
let a = (A 0)
local a* = a # because it's a plain struct, this is just a copy
let b = (B 1)
local b* = b # for a non plain struct, this is a move.
print a.a
# if you uncomment this, we get a lifetime error, because b has been moved into b*
# print b.b
b* = (B 2) # this moves a new B into b*, dropping the previous one
fn printb (b)
print b.b
# you can pass a non plain value through a function. It'll be automatically borrowed, and if
not spent (moved or destroyed) inside the function, the compiler infers that it can be returned.
printb b*
print b*.b
fn printb-die (b)
print b.b
move b # or imagine we store it in another mutable variable, or drop it
# will error due to value being spent already
# printb-die b*
# print b*.b
let b10 = (B 10)
let b9 = (B 9)
let b8 = (B 8)
let b7 = (B 7)
let b6 = (B 6)
;
# at the end of the scope (in this case the end of the module) all unique values are dropped
in the reverse order as they were defined (this is important to preserve dependencies).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment