v icon indicating copy to clipboard operation
v copied to clipboard

We are allowed to create structs time.Time

Open brandonpille opened this issue 2 years ago • 5 comments

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

brandonpille avatar Feb 14 '23 15:02 brandonpille

Easily accomplished by adding the [required] attribute to the Time fields.

JalonSolov avatar Feb 14 '23 16:02 JalonSolov

@JalonSolov there's [noinit] struct Time {} for that

medvednikov avatar Feb 14 '23 23:02 medvednikov

Or that. :-) Yes, that would be simpler - just prevent you from creating your own instance.

JalonSolov avatar Feb 15 '23 01:02 JalonSolov

I'll pick it up

paul-elesin avatar Feb 25 '23 19:02 paul-elesin

#17162 dup

depperm avatar Mar 30 '23 14:03 depperm

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 variable today, hours are 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

visrut-at-incubyte avatar Nov 24 '23 12:11 visrut-at-incubyte