Giter VIP home page Giter VIP logo

Comments (5)

andrewrk avatar andrewrk commented on May 14, 2024

we also want "volatile" and perhaps other variable qualifiers. maybe wrappable keyword is in order and you can put multiple variable qualifiers in front of the type? instead of iw32, wrappable i32

from zig.

MovingtoMars avatar MovingtoMars commented on May 14, 2024

IMO, there shouldn't be two separate types for wrapping and non-wrapping integers. It would be better to choose a sane default (wrapping) and make it the only option.

from zig.

andrewrk avatar andrewrk commented on May 14, 2024

I argue that not wrapping by default is actually safer, less likely to result in bugs, and produces more optimal code. Here's why:

Zig plans to have significantly different debug and release builds. For example, it's planned to have array bounds checking in debug mode but not in release mode. Uninitialized memory is set to all 0xaa bytes in debug mode, uninitialized in release mode. And finally, in debug mode, all integer arithmetic is checked and the program will crash on overflow. In release mode, overflow is undefined behavior.

Programmers rarely want integers to overflow. By default, we want an overflow to crash the debug build. Only when a programmer explicitly states that they want the wrapping behavior - perhaps they're doing bit fiddling, creating a hash algorithm, or a pseudo random number generator - will they get it. On the other hand, if all integers were wrapping, we wouldn't know if the wrapping was intentional or not, so we couldn't crash the debug build when it accidentally occurred.

As for optimal code, the ability to assume integers will not wrap provides more optimal integer arithmetic.

from zig.

ddevault avatar ddevault commented on May 14, 2024

I suggest using overflow by default (because this is what low level programmers will expect and maps more nicely onto the actual behavior of the CPU), and introducing a checked keyword for checked math. Perhaps something like this:

@maxValue(u32) + 1 == @minValue(u32)
checked { @maxValue(u32) + 1 ?? 0 } == 0

I personally expect and prefer to have overflow by default, it seems saner to me. Programmers should understand the underlying nature of their integers. Otherwise, I would prefer something like unchecked { ... } over the ugly macros.

from zig.

thejoshwolfe avatar thejoshwolfe commented on May 14, 2024

see #159 for the current state of this feature.

from zig.

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.