#Comptime different methods

1 messages · Page 1 of 1 (latest)

thin gale
#

Is there a way to define different methods on a struct at comptime depending on the type?
like

pub fn foo(comptime T: type) type {
    return struct {
        if (T == u8) {
            pub fn bar() u32 {
                return 1;
            }
            pub fn baz() u32 {
                return 2;
            }
        } else {
            pub fn bar() f32 {
                return 1.0
            }
            pub fn baz() f32 {
                return 2.0;
            }
        }
    }
}
cold garden
#
pub const bar = switch(T) {
  u8 => bar_u8,
  else => bar_other,
};

fn bar_u8() u32 {}
fn bar_other() f32 {}
thin gale
cold garden
#

will only compile error if they reference it

#

or you could do

pub usingnamespace switch(T) {
  u8 => struct {
    fn bar() u32 {}
  },
  f32 => struct {},
  else => struct {
    fn bar() u32 {}
  },
};