# Need help to debug sample code with 'inline else'. Getting a cast error

**URL:** <https://ziggit.dev/t/need-help-to-debug-sample-code-with-inline-else-getting-a-cast-error/1384>\
**Category:** Help\
**Created:** [August 5, 2023, 3:07pm UTC](https://ziggit.dev/t/need-help-to-debug-sample-code-with-inline-else-getting-a-cast-error/1384 "2023-08-05T15:07:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhishekkr](https://ziggit.dev/user_avatar/ziggit.dev/abhishekkr/32/806_2.png) [@abhishekkr](https://ziggit.dev/u/abhishekkr)\
**Post date:** [August 5, 2023, 3:07pm UTC](https://ziggit.dev/t/need-help-to-debug-sample-code-with-inline-else-getting-a-cast-error/1384/1 "2023-08-05T15:07:24Z")

</div>

Hi,

I’m new to Zig world, and trying to learn it via master’s language reference. Using `0.11.0-dev.4282+0f21d3d4d` linux x86-64 compiler.

I’m in “switch” section, and was writing a sample code with “inline else”. My error is of expected type though, am guessing I missed something simple… would appreciate help.

I’m posting sample code and error below. 🙏

```zig
const std = @import("std");
const debugPrint = std.debug.print;

pub fn main() !void {
    switch_inline_else();
}

fn switch_inline_else() void {
    const Cat = struct {
        const Self = @This();
        pub fn talk(self: *Self) void {
            debugPrint("{s} says Meow~\n", .{self.name});
        }

        name: []const u8,
    };
    const Dog = struct {
        const Self = @This();
        pub fn talk(self: *Self) void {
            debugPrint("{s} says Bark~\n", .{self.name});
        }

        name: []const u8,
    };

    const Animal = union(enum) {
        const Self = @This();
        pub fn talk(self: *Self) void {
            switch (self.*) {
                inline else => |case| case.talk(),
            }
        }

        cat: Cat,
        dog: Dog,
    };
    var dartDog: Dog = .{ .name = "Dart" };
    var dart: Animal = .{ .dog = dartDog };
    debugPrint("{any}\n", .{dart});
    dart.talk();
}

```

gives error of

```zig
/tmp/a.zig:30:43: error: expected type '*a.switch_inline_else.Cat', found '*const a.switch_inline_else.Cat'
                inline else => |case| case.talk(),
                                      ~~~~^~~~~
/tmp/a.zig:30:43: note: cast discards const qualifier
/tmp/a.zig:11:27: note: parameter type declared here
        pub fn talk(self: *Self) void {
                          ^ ~~~~
referenced by:
    switch_inline_else: /tmp/a.zig:40:9
    main: /tmp/a.zig:5:5

```

Edit.1: Also, any other notes on the code are appreciated as well.

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [August 5, 2023, 3:22pm UTC](https://ziggit.dev/t/need-help-to-debug-sample-code-with-inline-else-getting-a-cast-error/1384/2 "2023-08-05T15:22:47Z")

</div>

You are capturing `case` by value:

```zig
inline else => |case| case.talk(),

```

This is creating a local, constant copy.  
You can solve this by capturing by reference instead:

```zig
inline else => |*case| case.talk(),

```

I would however recommend a different approach:  
The talk function is not changing the input pointer, so there is no need to pass a mutable pointer here:

```zig
pub fn talk(self: Self) void { // Note: Don't forget to change all instances

```

This might even bring performance improvements since now the compiler is allowed to decide whether pass-by-value or pass-by-reference should be preferred.

---

<div class="post-metadata">

**Author:** ![abhishekkr](https://ziggit.dev/user_avatar/ziggit.dev/abhishekkr/32/806_2.png) [@abhishekkr](https://ziggit.dev/u/abhishekkr)\
**Post date:** [August 5, 2023, 3:43pm UTC](https://ziggit.dev/t/need-help-to-debug-sample-code-with-inline-else-getting-a-cast-error/1384/3 "2023-08-05T15:43:58Z")

</div>

@IntegratedQuantum thanks, `|*case|` capture be reference works.

I’ve also changed `*Self` to `Self`. I wrongly assumed, reference got passed to type’s functions.
