-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
WIP: checked integer conversions #8420
Changes from 1 commit
effff39
1d5050b
8495944
e1690fc
c67837c
06241fe
ec607da
50f1d10
16c2330
9f0ef0d
b3e30f9
3336fbc
4d1c138
000f41e
8c39ad2
8321a9e
53b6dee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,24 @@ vals = [ | |
typemax(Int64), | ||
] | ||
|
||
function coerce(T::Type, x) | ||
if T<:Rational | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return convert(T, coerce(typeof(num(zero(T))), x)) | ||
end | ||
if !(T<:Integer) || T===Bool | ||
convert(T, x) | ||
elseif sizeof(T) < sizeof(x) | ||
itrunc(T, x) | ||
elseif sizeof(T) == sizeof(x) | ||
reinterpret(T, x) | ||
else | ||
convert(T, x) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for completeness:
i think the above would generate better code, since it lets the compiler eliminate the tests and inline much better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, i see that this isn't in base, so it isn't as important. it seems like a pretty good name for it's missing a test for
|
||
|
||
for T=types, S=types, x=vals | ||
a = convert(T,x) | ||
b = convert(S,x) | ||
a = coerce(T,x) | ||
b = coerce(S,x) | ||
if (isa(a,Char) && !is_valid_char(a)) || (isa(b,Char) && !is_valid_char(b)) | ||
continue | ||
end | ||
|
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.
since assigned is a word, this seems like a poor method name
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.
You're right; I realized that afterwards. It would be better to get rid of both of these, but I don't have a better idea so far.
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 didn't have a suggestion either, but now i'm leaning towards
coerce(Unsigned,x)
being a good optionThere 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.
Why not
convert(Unsigned, x)
?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.
convert would throw an error if
x < 0