Skip to content

Latest commit

 

History

History
85 lines (51 loc) · 2.07 KB

00020-pointers_and_reference_syntax.md

File metadata and controls

85 lines (51 loc) · 2.07 KB

Pointers and Reference Syntax

  • Created: 2016-05-08

Summary

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

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

*int # pointer to type

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

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:

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

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.

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

Alternatives

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

Unresolved questions

Nothing so far.