Comments (5)
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.
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.
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.
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.
see #159 for the current state of this feature.
from zig.
Related Issues (20)
- std.posix.open runs into unreachable when used to open a tmp file HOT 2
- Use case: Ability to create global constructor/destructor functions HOT 2
- Failure to catch implicit break in block with anonymous struct HOT 1
- `std.debug.print("{}\n", .{1e.5});` should not compile HOT 1
- panic from `std.http.Server.Request.respond` when request exactly fills buffer and contains a body HOT 2
- Typo in `XChaChaIETF.stream()` HOT 1
- `mem.zeroes` thinks enum literals can be zeroed
- Improve translate-c macro `@compileError`s for pragma operators HOT 1
- flaky tests: FileNotFound
- Regression with source locations in stack traces on Windows HOT 3
- `std.compress.xz` does not implement `read` correctly (it treats end-of-stream as an error) HOT 2
- `std.mem.readVarInt` cannot handle return types with bit widths of 8 or fewer HOT 1
- 0.13 regression: simdutf targeting x86-64 no longer builds HOT 5
- sorting a list of structures makes zig compiler segfaults HOT 1
- "Argument unused during compilation" errors on AArch64 assembly files in 0.12.1 HOT 7
- @cVaArg() segfaults when it needs to retrieve a u128 from the stack (target = x86_64-Linux)
- Generated ELF says it needs libc.so but should ask for libc.so.6 (?) HOT 4
- Cross-build `x86_64-linux-gnu` -> `aarch64-linux-gnu` fails in langref steps
- missing update operators (<op>=) HOT 8
- It's unclear which minimum Linux kernel version the Zig standard library actually supports HOT 2
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 zig.