Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Byref as a new Type #35

Open
Victorious3 opened this issue Nov 13, 2023 · 1 comment
Open

Byref as a new Type #35

Victorious3 opened this issue Nov 13, 2023 · 1 comment
Labels
proposal New feature or request
Milestone

Comments

@Victorious3
Copy link
Contributor

The goal is to be able to write functions that accept a pointer, reference or a value. This is necessary in cases where you write data structures that can be either allocated by a reference, be on the stack or even allocated by another allocator (and be returned as a pointer).
To be able to encompass all of these uses the most straight forward way would be to just use a pointer. However, this requires a cast when coming from a reference and for values you need to use the address operator. Another downside is that there's no way to check if the value is used sensibly (i.e it doesn't escape the function).
Therefore it makes sense to use a new type byref T. This type would only be valid as function argument, and it would be implemented like a regular pointer. This also means that dynamic dispatch doesn't work for it, if you want that you need to use an ordinary reference. Here it works like a normal value.

type I = interface { def fun }

type S = struct {}

def foo(s: byref S) {}
// This also implicitly generates the function:
// def foo(s: &S) { foo(s !*S) }

let a: &I = {} !&S
a.foo() // Works, calls the right function

let b = {} !S
b.foo()

let c = *b
c.foo()
@Victorious3 Victorious3 added the proposal New feature or request label Nov 13, 2023
@Victorious3
Copy link
Contributor Author

Victorious3 commented Nov 17, 2023

After some consideration I would use

def foo(byref a: S)

instead to make sure that this is only valid for function arguments.

@Victorious3 Victorious3 added this to the 0.4 milestone Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant