Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We are allowed to create structs time.Time #17311

Closed
brandonpille opened this issue Feb 14, 2023 · 6 comments
Closed

We are allowed to create structs time.Time #17311

brandonpille opened this issue Feb 14, 2023 · 6 comments
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Good First Issue (easy task) This issue is suitable to be worked on by new contributors.

Comments

@brandonpille
Copy link
Contributor

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 brandonpille added the Bug This tag is applied to issues which reports bugs. label Feb 14, 2023
@JalonSolov
Copy link
Contributor

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

@JalonSolov JalonSolov added the Good First Issue (easy task) This issue is suitable to be worked on by new contributors. label Feb 14, 2023
@Delta456 Delta456 self-assigned this Feb 14, 2023
@medvednikov
Copy link
Member

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

@JalonSolov
Copy link
Contributor

JalonSolov commented Feb 15, 2023

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

@paul-elesin
Copy link
Contributor

I'll pick it up

@depperm
Copy link
Contributor

depperm commented Mar 30, 2023

#17162 dup

@visrut-at-incubyte
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Good First Issue (easy task) This issue is suitable to be worked on by new contributors.
Projects
None yet
Development

No branches or pull requests

8 participants