eql
Compares two of any type for equality. Containers that do not support comparison on their own are compared on a field-by-field basis. Pointers are not followed.
Function parameters
Parameters
- a:anytype
- b:@TypeOf(a)
Functions in this namespace
Functions
- stringToEnum
- Returns the variant of an enum type, `T`, which is named `str`, or `null` if no such variant exists.
- alignment
- Returns the alignment of type T.
- Child
- Given a parameterized type (array, vector, pointer, optional), returns the "child type".
- Elem
- Given a "memory span" type (array, slice, vector, or pointer to such), returns the "element type".
- sentinel
- Given a type which can have a sentinel e.g.
- Sentinel
- Given a "memory span" type, returns the same type except with the given sentinel value.
- declarations
- Instead of this function, prefer to use e.g.
- tags
- Given an enum or error set type, returns a pointer to an array containing all tags for that
- FieldEnum
- Returns an enum with a variant named after each field of `T`.
- activeTag
- Returns the active tag of a tagged union
- TagPayloadByName
- Deprecated: Use @FieldType(U, tag_name)
- TagPayload
- Deprecated: Use @FieldType(U, @tagName(tag))
- eql
- Compares two of any type for equality.
- intToEnum
- Deprecated: use `std.enums.fromInt` instead and handle null instead of an error.
- fieldIndex
- Given a type and a name, return the field index according to source order.
- declList
- Returns a slice of pointers to public declarations of a namespace.
- ArgsTuple
- For a given function type, returns a tuple type which fields will
- Tuple
- For a given anonymous list of types, returns a new tuple type
- isError
- Returns whether `error_union` contains an error.
- hasFn
- Returns true if a type has a namespace and the namespace contains `name`;
- hasMethod
- Returns true if a type has a `name` method; `false` otherwise.
- hasUniqueRepresentation
- True if every value of the type `T` has a unique bit pattern representing it.
Error sets in this namespace
Error Sets
- IntToEnumError
- Deprecated: use `std.enums.fromInt` instead and handle null.
Source
Implementation
pub fn eql(a: anytype, b: @TypeOf(a)) bool {
const T = @TypeOf(a);
switch (@typeInfo(T)) {
.@"struct" => |info| {
if (info.layout == .@"packed") return a == b;
inline for (info.fields) |field_info| {
if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false;
}
return true;
},
.error_union => {
if (a) |a_p| {
if (b) |b_p| return eql(a_p, b_p) else |_| return false;
} else |a_e| {
if (b) |_| return false else |b_e| return a_e == b_e;
}
},
.@"union" => |info| {
if (info.tag_type) |UnionTag| {
const tag_a: UnionTag = a;
const tag_b: UnionTag = b;
if (tag_a != tag_b) return false;
return switch (a) {
inline else => |val, tag| return eql(val, @field(b, @tagName(tag))),
};
}
@compileError("cannot compare untagged union type " ++ @typeName(T));
},
.array => {
if (a.len != b.len) return false;
for (a, 0..) |e, i|
if (!eql(e, b[i])) return false;
return true;
},
.vector => |info| {
var i: usize = 0;
while (i < info.len) : (i += 1) {
if (!eql(a[i], b[i])) return false;
}
return true;
},
.pointer => |info| {
return switch (info.size) {
.one, .many, .c => a == b,
.slice => a.ptr == b.ptr and a.len == b.len,
};
},
.optional => {
if (a == null and b == null) return true;
if (a == null or b == null) return false;
return eql(a.?, b.?);
},
else => return a == b,
}
}