# Support switch on string variable

**URL:** <https://ziggit.dev/t/support-switch-on-string-variable/7680>\
**Category:** Brainstorming\
**Tags:** language\
**Created:** [January 4, 2025, 3:33pm UTC](https://ziggit.dev/t/support-switch-on-string-variable/7680 "2025-01-04T15:33:07Z")\
**Posts on this page:** 1\
**Showing post:** 24

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 27, 2025, 5:25am UTC](https://ziggit.dev/t/support-switch-on-string-variable/7680/24 "2025-02-27T05:25:34Z")

</div>

> [@chung-leong](#):
>
> There’s nothing wrong with using series of if statements.

It can do more work than necessary, have worse performance and high variance in execution time between the first and last case.

> [@chung-leong](#):
>
> For situations where a hash table is warranted, a hash table of function pointers would make better sense.

I am very skeptical, I think function pointers can be a lot more costly than using a switch on a value. I don’t see why you would use function pointers when you instead could leave it up to the compiler.

> [@chung-leong](#):
>
> Looking up a value just to send it immediately to a switch is kinda silly.

Why?  
I think it is quite a common thing to create or get a value to switch over.

I don’t really see it as a lookup, I see it as a mostly comptime transformation that reduces a big type to a small type, similar to this [Advanced use of comptime: Tagged Union Subsets](https://ziggit.dev/t/advanced-use-of-comptime-tagged-union-subsets/6114), but if it is done with enums that both have the same backing integer and use the same values it should essentially be a `@enumFromInt(@intFromEnum(x))`, because there is no payload that needs to be copied.

With [Bitcast should be allowed on enums with tag types](https://ziggit.dev/t/bitcast-should-be-allowed-on-enums-with-tag-types/7674) it could be done with a bitcast.

---

_[View the full topic](https://ziggit.dev/t/support-switch-on-string-variable/7680)._
