1

Is there a syntax to auto generate constructors from inline initial values of object-type's members in nim ?
In the like of C++11/java

c.f. Default constructor vs. inline field initialization

imaginary syntax:

type
  MyT = object of RootObj
    str* = "<initial>"

for the moment the build outputs

initialization not allowed here

v.oddou
  • 6,476
  • 3
  • 32
  • 63

1 Answers1

3

Since you have to write down the constructor explicitly, this too has to be done explicitly. You could write a macro to do it automatically though.

type
  MyT = object of RootObj
    str*: string

proc initMyT(str = "<initial>"): MyT =
  result.str = str
def-
  • 5,275
  • 20
  • 18