Hello everyone,

in the example below the initCar proc creates a new car and defines sensible 
default values. Now I decide to extend the car with a new attribute maxSpeed: 
Natural. By default the compiler will set the value of maxSpeed to 0 which 
doesn't make sense for cars. As module author I'm in charge to set a default 
for maxSpeed in the constructor initCar. With a lot of attributes one can loose 
track of them and I may forget to init the car with a maxSpeed of 100 . Hence 
my question:

Does Nim provide a pragma to ensure that all attributes of an object are 
provided during its creation using the Car()-syntax (here in initCar)?
    
    
    type
      Car = object
        numOfWheels: Natural
        numOfDoors: Natural
        color: string
        hadAccident: bool
        distanceDriven: Natural
        # and many more attributes ...
    
    # create car with default values provided by module author
    proc initCar(): Car =
      Car(
        numOfWheels: 4,
        numOfDoors: 4,
        color: "red",
        hadAccident: false,
        distanceDriven: 0
      )
    
    
    var c = initCar()
    # user wants a blue car
    c.color = "blue"
    
    
    Run

Reply via email to