Skip to content

Pointers and reference syntax #20

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions text/00020-pointers_and_reference_syntax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Pointers and Reference Syntax

- Created: 2016-05-08


## Summary
[Summary]: #summary

I propose the universal star `*` operator.
It works for all pointer related operations.

```rebuild
*x # pointer to variable/argument (address of)
x* # dereference pointer

*int # pointer to type
```


## Motivation
[Motivation]: #motivation

We need a basic set of syntax defined in order to present more in depth proposals to the language.

Pointer handling seems inevitable in a compiled language.


## Detailed design
[Detailed design]: #detailed-design

I propose the following syntax.

1. A star in front of something is read as "pointer to".
2. A star behind something is read as "dereference".

The first rule works in type expressions and everything that an address can be taken of.
For example: variables, arguments, functions and instance members.

The second rule only applies to expressions which evaluate to pointers.

These two rules are minimal and very consistent.
That should make it very easy to teach.

Some more examples:

```rebuild
fn f (x) -> bool; # declare a function f
def pf = *f # pointer to function f
pf*(n) # invocation of the function behind the pointer

type i5 = *(5 of int) # pointer to array
type p5 = 5 of *int # array of pointers
```


## Drawbacks
[Drawbacks]: #drawbacks

### The dereferencing star operator might be ambigous with multiplications.

It does not make much sense to multiply a pointer with something.
So this ambiguity should be easily resolvable.

### The dereferencing star is ambigous for function invocations.

It's unclear for the reader wether a function pointer is dereferenced or the function result is dereferenced.

To mitigate the problem it should be illigal to dereference a function result without brackets around the function.

```rebuild
var a = f()* # function returns a pointer that is dereferenced
```


## Alternatives
[Alternatives]: #alternatives

Endless other syntaxes are thinkable.
If we find one more pleasing or conflict with other ideas, we should adapt it.


## Unresolved questions
[Unresolved questions]: #unresolved-questions

Nothing so far.