Skip to content

lent/var/openArray inside objects/containers #178

Closed
@Araq

Description

Nim currently has 3 types that are second-class citizens due to memory safety reasons: var T, lent T, openArray[T]. These can only be used in parameter lists as the underlying implementation is that of a borrowed pointer that must not outlive the callee. See https://nim-lang.org/docs/manual.html#procedures-var-return-type for details about how this is done for var T.

This proposal is about extending the capabilities of these types so that they are allowed inside other types like objects or tuples. A type containing a borrowed pointer (var T / lent T / openArray) becomes borrowed too and a value of such a type must not outlive the location it was borrowed from. This is enforced statically. How exactly this static enforcement will work remains to be seen, but the rules of the first implementation will be:

  1. A borrowed type cannot be used for (local or global or threadlocal) variables or consts. It must be used as a parameter or return type.
  2. If used as a return type, the location must borrow from the first parameter that is passed to the proc. This can later on extended to use a from declaration.

In other words, exactly the same rules apply that apply for https://nim-lang.org/docs/manual.html#procedures-var-return-type.

ToDo: Show what idioms this extension allows for. This RFC is in desparate need of practical examples.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions