Skip to content

Support default values for object properties #126

@ghost

Description

Example:

type MyObj = object
    id: int = generateUniqueId()
    x: int = 42
    stuff: seq[string] = @[]

I can think at least the following benefits:

  • Faster prototyping (less typing when no need to write newMyObj or initMyObj procs)
  • Makes it harder to misuse objects (for example when using some library for the first time)
  • Easier generics (with a stupid example):
proc doSomethingGeneric[T](): T =
    assert result.x == 42
    assert result.stuff != nil

discard doSomethingGeneric[MyObj]()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions