#Zig generic programming question

1 messages ยท Page 1 of 1 (latest)

craggy thistle
#

imagine that TYPE can be replaced by anything, just like C++ <T>

hkNode_TYPE *hkList_TYPE_remove(hkList_TYPE *list, TYPE item) {
    if (list->length == 0) {
        return NULL;
    }
    hkNode_TYPE *result = NULL;
    hkNode_TYPE *iter = list->head;
    hkNode_TYPE *prev = NULL;
    while (iter) {
        if (iter->data == item) { // <- this line
...

this works fine for primitive types but once you have a composite type...
so since in C and Zig, there is no operator overloading, how would the equality operator work on non-primitive structure/composite types?
how would this be implemented in Zig?

hearty rampart
#

look into std.meta.eql - it can compare values of any type, and checks for field equality

mental radish
#

I don't think that's relevant

craggy thistle
#

yeah I was thinking it requires introspection

mental radish
#

I think what you are looking for is something like anytype or passing in the type of item

#

you don't have operator overloading so you'd have to do iter->data.eql(item) or something

#

where eql is a function on the type of iter->data

craggy thistle
#

yeah

mental radish
#

well I guess it would be kinda easy actually if the list already takes in a type parameter

craggy thistle
#

so I'd have to check if it is a primitive type or not
if not, invoke the (in this case) hkNode_TYPE_eql(hkNode_TYPE *a, hkNode_TYPE *b);

craggy thistle
mental radish
#
pub fn List(comptime T: type) type {
  return struct {
    ...,

    pub fn remove(self: *List(T), item: T) void {
      ...
        if (iter.data.eql(item)) {

        }
      ...
    }
  };
}
craggy thistle
#
//meta@hkArray:i32
hkArray_i32 *array = hkArray_i32_create(8);

the commented line is a directive for my meta gen program

mental radish
#

however I think this makes what fri3d said somewhat relevant

#

since if you just want to do a structural comparison then std.mem.eql is good

#

but if you specifically mean like

#

any sort of comparison implemented by operator overloading

#

then yea you would have to get creative with the types

lament forge
#

just include an isEqual function?

craggy thistle
#

got it. so either introspect, or create a function

lament forge
#

like a generic sorting algo in C would

craggy thistle
lament forge
#

either that or you can pass in a function directly

#

comptime

#

this would be nicer if zig had anonymous functions

craggy thistle
#

I think I should implement introspection and generate all the code for any struct

#

parse structs and stuff and generate procs for structural equality

#

I wish Dennis Ritchie did structural equality in C

craggy thistle
#

or just use Zig xD

lament forge
#

whats wrong with an eql function?

craggy thistle
#

nothing, im implementing this stuff in C, not zig

lament forge
#

why ๐Ÿฅฒ

craggy thistle
#

because I am a sadist

craggy thistle
lament forge
#

id argue writing zig macroes is more painfull

craggy thistle
lament forge
#

yes

craggy thistle
#

yeah, it has some rules....
im literally just parsing C and writing C

craggy thistle
lament forge
#

i suggest try zig.

#

comptime is just accessing parsed structs

#

but even thats not needed for this case

craggy thistle
craggy thistle
craggy thistle
craggy thistle
# hearty rampart look into `std.meta.eql` - it can compare values of any type, and checks for fie...

https://github.com/ziglang/zig/blob/ebd9efa85052fc19d8296e8e0f4da079da9cab45/lib/std/meta.zig#L741
I found this.
it says:

/// Compares two of any type for equality. Containers are compared on a field-by-field basis,
/// where possible. Pointers are not followed.

works perfectly for types that have value fields but say I have a struct string{ int len; char *data; }; then it wouldnt check the data coz its a ptr

#

I would have to invoke the proper string_eql() proc

hearty rampart
#

yeah, in that case you'd want to expect an equality method to be defined in the type, if it is composite - or use == for primitive types that support it...

craggy thistle
hearty rampart
#

why not?

craggy thistle
#

coz if I make one generic function, will I have to tell it what to do for each type?

craggy thistle
hearty rampart
#

in the C++ code you expect the type to overload the == operator - in the Zig code you expect the type to have an isEqual if it is composite, or == if primitive.

craggy thistle
#

oh ok

#

it'll just invoke isEqual gotcha

#

thats awesome

#

like, isEqual is "standard" for all types right?

hearty rampart
#

no. it does not exist for primitives.
you'll have to inspect the given type, if it is a composite type you can call isEqual on it (if it doesn't have such method there'll be a compilation error - as wanted). if it is a primitive you can use ==.

craggy thistle
#

yeah, I meant anything that was not a prim

hearty rampart
#

there is no conventional name for a user-created special equality function - you'll just have to inform your function's users to implement such method on their type, same as std.fmt.format wants you to define a method called format, of a specific signature, if you want user-defined formatting for your type.

craggy thistle
#

ok

#

purely in the sense of standardization, I think operator overloading wins here since everyone will just implement == instead of eql isEqual is_equal etc....

#

but then again, to hell with operator overloading ๐Ÿ˜‚

#

I assume the same goes for greaterThan lessThan etc. ?

hearty rampart
#

you can also be generic over the defined methods, accepting another argument to the function: the comparison function. this is what std.HashMap does, though it wraps the two needed functions (eql and hash in a "Context" type)

craggy thistle
#

yeah, I suppose thats a function ptr

craggy thistle
hearty rampart
hearty rampart
craggy thistle
#

thanks so much!!

#

you gave me ideas to solve my ultra hacky C metaprogramming stuff ๐Ÿ˜‚

hearty rampart
#

C metaprogramming :(

craggy thistle
craggy thistle
hearty rampart
#

I should make me a keyboard macro for these embedded links... am writing them manually wayyy too much

craggy thistle
#

took me a good 10 minutes to find std.meta.eql

hearty rampart
#

go here, simply search for the item you want

craggy thistle
#

Much Love! Peace be upon you! ๐Ÿ™