phobos/changelog/bit-cast.dd
Paul Backus 6722d0a50b
Add std.conv.bitCast (#10651)
* Add std.conv.bitCast

This helper function is a more readable alternative to the traditional
pointer-cast-based syntax for reinterpreting casts.

The name bitCast is used for this operation in several other languages,
including C++ [1], Swift [2], C# [3], Zig [4], and LLVM IR [5].

[1] https://en.cppreference.com/w/cpp/numeric/bit_cast
[2] https://developer.apple.com/documentation/swift/unsafebitcast(_:to:)
[3] https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafe.bitcast
[4] https://ziglang.org/documentation/0.10.0/#bitCast
[5] https://llvm.org/docs/LangRef.html#bitcast-to-instruction

* Add changelog entry for std.conv.bitCast
2025-02-28 13:54:10 -08:00

14 lines
282 B
Text

Added `std.conv.bitCast`
This convenience function allows reinterpreting casts to be written in a more
readable way.
---
uint n = 0xDEADBEEF;
// Before
writeln("Bytes of n are: ", *cast(const ubyte[4]*) &n);
// After
writeln("Bytes of n are: ", n.bitCast!(const ubyte[4]));
---