diff --git a/text/00020-pointers_and_reference_syntax.md b/text/00020-pointers_and_reference_syntax.md new file mode 100644 index 0000000..6ea1eb0 --- /dev/null +++ b/text/00020-pointers_and_reference_syntax.md @@ -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.