Giter VIP home page Giter VIP logo

Comments (4)

straight-shoota avatar straight-shoota commented on May 23, 2024 1

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.

ysbaddaden avatar ysbaddaden commented on May 23, 2024 1

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.

konovod avatar konovod commented on May 23, 2024

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.

straight-shoota avatar straight-shoota commented on May 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.