Skip to content

Pointer Reform #770

Closed
Closed
@andrewrk

Description

@andrewrk

EDIT


& only used for address-of, no longer designates a pointer type. Necessary because of #588

  • ^ pointer to exactly 1 thing.
  • [*] pointer to a block of memory of unknown length
  • [*]null pointer to block of memory, null-terminated (or 0 terminated for integers). proposal: type for null terminated pointer #265
  • [] pointer to a block of memory with runtime known length. status quo slices.
  • []null pointer to a block of memory with runtime known length, with a null/0 at ptr[len]
  • [N] pointer to a block of memory with comptime known length
  • [N]null pointer to a block of memory with comptime known length, and a null/0 at ptr[N]

All of them support pointer indexing and slicing except ^. Only [*] supports pointer arithmetic. All of them implicitly cast to [*]. []null and [N]null implicitly cast to [*]null.

  • &ptr[x] and &foo always gives a ^.
  • ptr[x..y] with comptime known x and y gives a [N].
  • array[x..] gives a [N].

new array syntax

var array: 4*i32 = undefined;

Now it is clear whether you should do &array or &array[0]. Don't use &array. If you want a [N]T, e.g. a pointer with comptime known length, use array[0..]. If the function wants to access more than one element, you'll do this. Otherwise, &array[0], will give ^T, which would trigger a compile error if the array was length 0, and only this element can be accessed via this pointer.

This paves the way for #733
See also #386
See also #568

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions