When I include the DateTime into the object, I cannot use the object freely because the compiler always complain that the DateTime field is not initialized.
type
User = object
name: string
age: int
birthday: DateTime
var u = User(name: "John", age: 20) # ERROR! field not initialized: birthday
var x: User # Warning: Cannot prove that 'x' is initialized
The simple workround is use Time instead, but not very convenient.
Is there any good way to solve the problem?
This happens because the default value for all types is binary 0, which is invalid for the Month type which starts at 1. Hopefully the compiler will be changed in the future so that enum/range types are instead initialized to their lowest value. Since string and seq will soon be lazily initialized to "" and @[], the rule that everything is initialized to 0 wont be true anyway.
As a workaround, you can write a constructor for User that initializes birthday, so you at least don't need to deal with it everywhere.
@GULPF
I need to define the variable first, then initialize the object later according to different condition. And when other object reference this object with DateTime, they need to be initialized when variable define if we don't want to see the warning.
In nim, variable can be defiend as:
var obj: T
But with the DateTime, it gives warning.
Maybe implementation of DateTime should make the month start from 0, then everything seems fine.