#creating a generic type and restricting its inputs

1 messages · Page 1 of 1 (latest)

covert quartz
#

this is my first try using generics directly, and I am having trouble restricting the type to numerics and Bools only, this is the snippet

pub fn Foo(comptime T: type) type {
  return struct {
    const Self = @This;
    fizz: T,
    buzz: T,

    pub fn init(fizz:T, buzz:T) Self {
      return .{.fizz = fizz, .buzz = buzz};
    }
  }
}
#

creating a generic type and restricting its inputs

west basin
#

Before the return put

comptime {
    switch (@typeInfo(T)) {
        .int, .@"bool" => void,
        else => @compileError("Only bools and int allowed"),
    }
}
warped isle
warped isle
covert quartz
#

?????

#

i did upper

west basin
warped isle
covert quartz
#

also thanks for showing bool

covert quartz
#

😭 😭

warped isle
#

it doesn't unless you're looking at old version

covert quartz
covert quartz
warped isle
# covert quartz

No idea what this "completion" is. Don't trust it from now on I guess

#

Actually no, it's correct

covert quartz
#

🥀 aight