www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Upcoming bitfield and adam's call to make it even better

reply ryuukk_ <ryuukk.dev gmail.com> writes:
If you didn't know, bitfields just landed in the latest beta!

https://dlang.org/changelog/2.101.0.html#dmd.bitfields

Definitely a huge improvement over the current way of doing 
bitfields (import module + template) (in my opinion)

However, in his blog, adam has some interesting concerns about 
it, and shared a proposal on how it could be made better

I personally like his proposal, i would love to hear what Walter 
thinks about it!

It takes what C offers, and make it safer to use! it's exactly a 
BetterC language!

(btw, we could do the same for tagged union and .enum ;) ;) ;) ;) 
;) ;) ;), just dropping this idea here)


Here is the link of adam's blog post: 
http://dpldocs.info/this-week-in-d/Blog.Posted_2022_09_12.html#d-bitfields-could-be-good
Oct 18 2022
next sibling parent reply Dave P. <dave287091 gmail.com> writes:
On Tuesday, 18 October 2022 at 19:11:35 UTC, ryuukk_ wrote:
 If you didn't know, bitfields just landed in the latest beta!

 [...]
As I said in the other thread, I think we need both. Both extern(C) bitfields to match what C is doing and D bitfields that can actually be good and portable across different systems/ABIs.
Oct 18 2022
parent test123 <test123 gmail.com> writes:
On Tuesday, 18 October 2022 at 21:07:51 UTC, Dave P. wrote:
 On Tuesday, 18 October 2022 at 19:11:35 UTC, ryuukk_ wrote:
 If you didn't know, bitfields just landed in the latest beta!

 [...]
As I said in the other thread, I think we need both. Both extern(C) bitfields to match what C is doing and D bitfields that can actually be good and portable across different systems/ABIs.
extern(C) bitfields could be very userful when you need translate c to d
Oct 20 2022
prev sibling parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Tuesday, 18 October 2022 at 19:11:35 UTC, ryuukk_ wrote:
 Here is the link of Adam's blog post: 
 http://dpldocs.info/this-week-in-d/Blog.Posted_2022_09_12.html#d-bitfields-could-be-good
The syntax Adam proposes is this: ```d ulong fields { msb : 1, _reserved: 62, lsb: 1, }; ``` Personally, I don’t like the idea of taking the syntax `Type Identifier { }` for something as niche as bit fields. If we ever close it unnecessarily. The fix is rather easy: Remove the name: ```d ulong { msb: 1; // semi colon _reserved: auto; // auto (allowed at most once) = ulong’s bits minus given bits. lsb: 1; } // no semi colon ``` It still should have a auto-generated name for the allMembers trait. If *you* want a name, use an anonymous union: ```d union { ulong bitfields; ulong { msb: 1; _reserved: auto; lsb: 1; } } ```
Oct 20 2022