Skip to content

Proposal: rename/modify hasFn and hasField trait functions #8240

Closed
@Airtz

Description

@Airtz

Motivation

As of now std.meta.trait.hasFn and std.meta.trait.hasField are matching on members identifiers alone.

This is not very usefull to express shared behavior.

Proposal

Match on members signatures instead.

  1. Remove hasFn(comptime name: []const u8) in favor of a more generic hasDecl(comptime name: []const u8, comptime T: type)
  2. Change hasField(comptime name: []const u8) to hasField(comptime name: []const u8, comptime T: type)

Usage

const trait = @import("std").meta.trait;

const isMyTrait = trait.multiTrait(.{
    trait.hasField("my_field", u8),
    trait.hasDecl("my_const", []const u8),
    trait.hasDecl("myFn", fn(bool, u8) f64),
});

Related issues

#3839 (naming coherence)
#1268 (use case)

Metadata

Metadata

Assignees

No one assigned

    Labels

    standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions