Last active
December 12, 2015 00:48
-
-
Save toffaletti/4686420 to your computer and use it in GitHub Desktop.
Low Level Language Thoughts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Finalizer Logic | |
struct Ptr { | |
void *value; | |
} | |
Finalize(Ptr ptr) { | |
free(ptr.value); | |
} | |
Ptr Example() { | |
Ptr a{malloc(42)}; | |
Ptr b{malloc(32)}; | |
// "b" does not leak because Finalize (dtor) is called when value doesn't escape scope | |
// Finalize is *not* called for "a" because it escapes | |
return a; | |
} | |
Ptr a = Example(); | |
// no double free because Finalize is called once for "a" here | |
Syntax Experiments | |
// Example of a unique_ptr | |
struct UniquePtr[T] { | |
value : unsafe.Ptr[T] | |
} | |
Finalize(ptr : UniquePtr[T]) { | |
unsafe.Free(ptr.value) | |
} | |
// Example of reference counter pointer | |
struct SharedPtr[T] { | |
value : unsafe.Ptr[T] | |
refc : unsafe.Ptr[uint] | |
} | |
SharedPtr(value : unsafe.Ptr[T]) { | |
return SharedPtr(value, new uint(0)); | |
} | |
Copy(ptr : SharedPtr[T]) -> SharedPtr[T] { | |
ptr.refc += 1 | |
SharedPtr(ptr.value, ptr.refc) | |
} | |
Finalize(ptr : SharedPtr[T]) { | |
ptr.refc -= 1 | |
if ptr.refc == 0 { | |
unsafe.Free(ptr.value) | |
unsafe.Free(ptr.refc) | |
} | |
} | |
= Goals = | |
C like control and performance | |
C compatibility (call from C, call into C) | |
Improve Safety | |
strict static typing | |
single assignment | |
const by default | |
Automatic Finalizers - resource management | |
Pattern matching | |
Generics | |
Make unsafe code stand out - explicit | |
require mutable keyword when mutating state | |
pointer operations | |
Make static analysis easier | |
Type-safe variatics |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment