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)
- Callable structs HOT 2
- error: NoSpaceLeft with MacOS target HOT 1
- Allow HTTP client timeout option
- error: unable to resolve inferred error set for a simple program HOT 2
- Windows: Prefer `ntdll` named pipe APIs over `kernel32`
- std.dynamic_library.DlDynLib does not give useful errors
- Multiple integer overflow errors in std.hash_map HOT 3
- Zig CC failing to compile treesitter plugins if spaces aren't separating flags and values
- Compiler segfaults at "zig`InternPool.indexToKey + 56" when compiling included code in 0.11.0, not in 0.10.0 HOT 1
- About Windows named pipe APIs missing HOT 1
- can not access `packed struct` members HOT 1
- dependency fetch hangs indefinitely HOT 2
- Missing atomic builtins for RP2040 (cortex_m0plus) HOT 5
- Comptime-known block result with preceding runtime branch is permitted only when break is trailing HOT 1
- ship even more of the compiler in source form HOT 7
- `zig build` hangs trying to fetch dependencies, 'zig fetch` crashes with SIGSEGV HOT 9
- emscripten: OutOfMemory bug when using default page_allocator
- I can't init a project on Windows HOT 1
- `error.InvalidHandle` has confusing/wrong usage in places
- for loop as expression doesnt accept implicit break from value 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 zig.