#Incompatible type with same signature

1 messages · Page 1 of 1 (latest)

cold basin
#

I know that zig doesn'g support incompatible type, for example, I'd like the following to NOT compile:

const A = u64;
fn add(a: A, b: A) A {
    return a + b;
}

comptime {
    var a: u64 = 2;

    var d = add(a, 4);
    _ = d;
}

I'd like add to only accept variables declared with type A explicitly.

What do you recommend to implement this with the current zig features?

trim vortex
#

const A = enum(u64) { _ };

upper dagger
#

beat me to it

trim vortex
#

quicktype

upper dagger
#

That only works for integral types, and only when you don't want the same operations as the underlying type

#

But it's nonetheless handy

cold basin
#

aah nice to know, I was already elaborating something more complicated.

trim vortex
#

like what?

cold basin
#

a struct with a unique field name

trim vortex
#

well, that's also a valid way to do it

cold basin
#

the enum trick is very handy for ints, and is what I need now.