v
v copied to clipboard
We are allowed to create structs time.Time
Describe the bug
It is possible to create time.Time objects which might introduce problems when using the object. For example the code below does not do what you want it to do:
now := time.now()
today := time.Time { year: now.year, month: now.month, day: now.day }
today_5am := today.add(time.hour*5)
You expect the object today_5am to be today 5 AM but it is actually 1970-01-01 05:00:00. That is because the unix property was not filed in while creating the object. Suggestion: don't allow the construction of the object outside of the module
Expected Behavior
See description
Current Behavior
See description
Reproduction Steps
See description
Possible Solution
See description
Additional Information/Context
No response
V version
V 0.3.2
Environment details (OS name and version, etc.)
OS: linux, Ubuntu 22.04.1 LTS Processor: 16 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
getwd: /work/farmerbot vmodules: /home/brandon/.vmodules vroot: /work/v vexe: /work/v/v vexe mtime: 2023-01-18 13:03:15 is vroot writable: true is vmodules writable: true V full version: V 0.3.2 f0a252d.2c78078
Git version: git version 2.34.1 Git vroot status: weekly.2023.03-15-g2c780788 (152 commit(s) behind V master) .git/config present: true thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
Easily accomplished by adding the [required] attribute to the Time fields.
@JalonSolov there's [noinit] struct Time {} for that
Or that. :-) Yes, that would be simpler - just prevent you from creating your own instance.
I'll pick it up
#17162 dup
We can add [noinit] before the struct, but it could introduce breaking changes and I can see there are some tests that are failing.
- If the user is intending the behavior like
today:= time.Time { year: now.year, month: now.month, day: now.day }, they are assuming in variabletoday,hoursare also being automatically set. - We can implement this behavior but, I don't know how to run some operation after the user initiates the object, something like a constructor function where we can set value for
hour