Bug 14795 – Allow user types implementing opEquals/toHash in switch statements

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-07-12T09:21:20Z
Last change time
2024-12-13T18:43:45Z
Assigned to
No Owner
Creator
Yuxuan Shui
Moved to GitHub: dmd#19015 →

Comments

Comment #0 by yshuiv7 — 2015-07-12T09:21:20Z
Can we extent switch statements to support (compile-time known) values with type other than {,w,d}char[] or int? I suggest we can support any types that implement toHash and opEquals. Switch statements like this: switch(a) { case a1: break; case a2: break; case a3: break; ... } are thus lowered to: switch(a.toHash) { case a1.toHash: if (a1 == a) {...} break; case a2.toHash: if (a2 == a) {...} break; case a3.toHash: if (a3 == a) {...} break; ... } Hash collisions can be resolved into an if-ladder.
Comment #1 by robert.schadek — 2024-12-13T18:43:45Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19015 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB