-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
restrict numeric promotions to cases where no data is lost #4971
Conversation
I personally prefer the way V works now. It is simpler and more predictable, since you just have to remember one rule to understand what the result would be, and what its type would be. |
@spytheman : I understand your argument but there are really wired examples like:
or even:
I don't think that this behavior is accepted/understood by many users. Think of someone unbiased just having heard of |
There are of cause other possibilities to address this:
|
I know, and understand that most languages do automatic promotions. It is just a personal preference, and if most people expect it/want it, I guess it is good for V to provide it. |
Yeah V is like Go, no magic casting rules, everything must be explicit. I'm considering only allowing automatic casting from a smaller to a larger type like |
@medvednikov Actually, I like the way Go handles this, too. But this would mean that a lot of existing What about this scheme:
There is no data loss but things like |
Yeah this is fine, numeric consts are also going to be untyped. |
I've basically implemented the above type scheme where only lossless automatic promotions are allowed. Also there are now two "untyped" types |
cmd/tools/modules/testing/common.v
Outdated
@@ -50,7 +50,7 @@ pub fn new_test_session(_vargs string) TestSession { | |||
skip_files: skip_files | |||
vargs: vargs | |||
show_ok_tests: !_vargs.contains('-silent') | |||
message_handler: 0 | |||
message_handler: &TestMessageHandler{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be &TestMessageHandler(0)
to avoid an extra allocation.
Maybe we should use &TestMessageHandler(none)
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used &TestMessageHandler(0)
for now. But I agree that optionals would be better alternatives in such cases.
.github/workflows/ci.yml
Outdated
run: | | ||
git clone --depth 1 https://github.com/vlang/vid.git | ||
cd vid && ../v -o vid . | ||
# - name: Test vid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be brought back now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Back in...
Great job, looks good. |
I think, that can be merged now. There is potential for some more fine tuning and merging of checks that can be addressed in future PRs. |
Great job @UweKrueger ! |
Problem
Numeric calculations involving different types give unexpected results. Example:
Reason
v
assumes the type of an expression to be the type of the left hand side, whereas the underlying C performs numeric promotions. These two concept do not fit very well together.Solution (updated)
This PR restricts numeric promotions to those where the target type is as strict superset of the original type as described in the scheme below. In brief: when there is no risk of data loss.
To handle integer and float literals two new internal types
any_int
andany_float
are introduced that can convert to any type of their class, so things likea := 3 + byte(b)
orx := 3.2 * (f32(y) + 4)
work in the way thata
is abyte
andx
is af32
.