# Switch on @TypeOf anytype argument

**URL:** <https://ziggit.dev/t/switch-on-typeof-anytype-argument/16588>\
**Category:** Help\
**Tags:** language\
**Created:** [July 8, 2026, 7:13pm UTC](https://ziggit.dev/t/switch-on-typeof-anytype-argument/16588 "2026-07-08T19:13:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rosew0od](https://ziggit.dev/user_avatar/ziggit.dev/rosew0od/32/7663_2.png) [@rosew0od](https://ziggit.dev/u/rosew0od)\
**Post date:** [July 8, 2026, 7:13pm UTC](https://ziggit.dev/t/switch-on-typeof-anytype-argument/16588/1 "2026-07-08T19:13:44Z")

</div>

Hi, this function fails with the error `error: operand of switch loop has comptime-only type 'lang.Type'`:

```zig
fn msgLen(msg: anytype) u16 {                                                   
    const T = @TypeOf(msg);                                                     
                                                                                
    var size: usize = 0;                                                        
                                                                                
    sw: switch (@typeInfo(T)) {                                                 
        .@"struct" => |info| for (info.field_types, info.field_names) |ty, name| {
            switch (ty) {                                                       
                i32, u32, ObjectId => size += @sizeOf(ty),                         
                []const u8 => size += @sizeOf(u32) + roundUp(4, @field(msg, name).len),
                else => unreachable,                                               
            }                                                                      
        },                                                                         
        .@"union" => continue :sw @field(msg, @tagName(msg)),                      
        else => unreachable,                                                       
    }                                                                              
                                                                                   
    return @intCast(size);                                                         
} 

```

Is there a way around this, or do I have to pass the type as an argument?

---

<div class="post-metadata">

**Author:** ![xash](https://ziggit.dev/user_avatar/ziggit.dev/xash/32/5384_2.png) [@xash](https://ziggit.dev/u/xash)\
**Post date:** [July 8, 2026, 7:36pm UTC](https://ziggit.dev/t/switch-on-typeof-anytype-argument/16588/2 "2026-07-08T19:36:23Z")

</div>

`@TypeOf` of an `anytype` is not the problem.

Without being able to test it because surrounding functions are missing, but `continue :sw @field(msg, @tagName(msg))` is likely a different type than the type of `@typeInfo(T)`? Did you mean to `continue :sw @typeInfo(@FieldType(msg, @tagName(msg)))` or something like that?

Otherwise a recursive function call might be easier to understand & give better error messages right now than a switch-loop.

---

<div class="post-metadata">

**Author:** ![rosew0od](https://ziggit.dev/user_avatar/ziggit.dev/rosew0od/32/7663_2.png) [@rosew0od](https://ziggit.dev/u/rosew0od)\
**Post date:** [July 8, 2026, 10:53pm UTC](https://ziggit.dev/t/switch-on-typeof-anytype-argument/16588/3 "2026-07-08T22:53:38Z")

</div>

Thank you! Using both a recursive function, and an inline for loop comparing to the tagName rather than using tagName directly worked!
