Comments (4)
Yeah, this behaviour is expected and (currently) intended. I agree that it can be confusing, though.
The underlying idea is that performing an operation on a variable should not change the type of the variable. This is particularly relevant for the assignment operators, such as a *= b
(which is equivalent to a = a * b
). It would be weird if the type of a
was changed by this operation.
So the math operators ensure that typeof(a * b)
is always equal to typeof(a)
.
Knowing this practical reason, I believe the behaviour is quite comprehensible. But it's not without alternatives.
It's just that this solution was chosen for Crystal and we have to live with it.
I think this could potentially be changed in the future. I would support that because it's an odd rule and confusing.
But I'm not sure how much effort it would be. However, I'm confident that we can find an alternative that is more approachable and less surprising, and probably won't break too much of existing code. It would still be a breaking change, though. So it's not an immediate concern.
from crystal.
It goes further than just the assignment operators. See #8111 and #8872.
IMO integers are broken in Crystal, or at least not as simple as they could have been; either you live with the default Int32 everywhere or you type everything when you need explicit sizes, down to each literal value 🤷
I wish this had been fixed before 1.0 but we instead kept the rule that the left-most type is the result type, so Int64 * Int32 returns an Int64 while Int32 * Int64 returns an Int32, which means that you should always write a * 1
instead of 1 * a
so you can keep the type of a
... unless you want the result to be an Int32...
from crystal.
Maybe a *= b
should be made equivalent to a = a.class.new(a*b)
? This would work with unions but maybe has runtime overhead (it can be optimized to old behavior when typeof(a*b) == typeof(a))
from crystal.
Yeah, that could be an option.
I'd like to look into requiring identical number types for math operations. Thus you would need to be explicit about type casting when the types don't match. Auto-casting should cover some basic use cases, so it wouldn't be too much noise.
from crystal.
Related Issues (20)
- Can't initialize a struct inside `#pre_initialize` HOT 4
- Unable to run interpreter after building HOT 2
- Using `lld-link` as a linker HOT 1
- Interpreter bug when `require "http/server"` HOT 1
- Can't instantiate a UNIXSocket instance in the interpreter HOT 2
- Some `#[]?(Range)` methods can still raise `IndexError`
- Interpreter hits Invalid memory access (after Error: BUG: no target defs) HOT 1
- Huge `JSON::ParseException` HOT 5
- `Number#humanize` and `Int#humanize_bytes` should separate number and unit by a space HOT 1
- Regression: Interrupted system call in CI HOT 6
- Is an html strip-tags method a worth stdlib addition? HOT 7
- Unwinding is slow on WSL HOT 8
- add something like ruby un.rb or cmake -E for cross platform scripts HOT 3
- regression: XPath swallows errors and raises opaque `error in [...] expression` instead HOT 1
- Proposal: Utility method for string truncation HOT 2
- Debugging Windows binaries with LLDB doesn't break on access violation
- Manage compiler dependencies with `shards` HOT 6
- Passing `Proc`s with non-default calling conventions across libs HOT 4
- Standardizing the type for timeout values HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crystal.