Achieve syntax of casts with auto return type deduction

I want to write function which automatically receives info about expected return type from call site like @ptrCast does.

Something like:

fn main() void {
   const t: usize = magic_fun();
   const t2: f16 = magic_fun();
}

fn magic_fun() magic.call_site.expected_type {
    switch(@typeInfo(magic.call_site.expected_type)) {
           "usize" => return 10;
           "f16" => return 1.995;
    }
}

I am fairly sure the only way to implement that would be by manually passing the type as an argument.
e.g. converting to

fn main() void {
   const t = magic_fun(usize);
   const t2 = magic_fun(f16);
}

fn magic_fun(T: type) T {
    switch(T) {
           usize => return 10,
           f16 => return 1.995,
           else => @compileError("Invalid type"),
    }
}
2 Likes

Me too. It’s currently impossible. I haven’t seen any accepted proposals either. I would love to use the capture syntax that’s gonna be used for anytype:

fn fun() |CallSite| {
   ...
}

In the meantime you have to pass it manually.

2 Likes