Skip to content

Relax generic function type erasure #6997

Closed

Description

@alexnask

Definitions

  • Generic function: A function is generic when at least one of its arguments is generic, or when it returns a compile time only type
  • Generic argument: An argument is generic when it is comptime or anytype

Current behavior

In stage1, generic function type erasure works in the following way:
If any of the arguments is generic, all of the following arguments are type erased, as well as the return type (erased types are represented as anytype in the typename of the type of the generic function)
If no arguments are generic but the return type is a compile time only type, the return type is type erased.

Proposal

If none of the arguments are generic but the return type is comptime only, the function shall be marked as generic but the return type shall not be type erased.
When at least one of the arguments is generic, the following shall be applied to the rest of the arguments and the return type:

  • If the type expression contains any references to a preceding generic or type erased argument, the current argument or return type shall be type erased
  • Otherwise, the argument or return type shall remain unmodified

Activity

  1. added
    proposal
    on Nov 5, 2020
  2. added this to the 0.8.0 milestone on Nov 5, 2020
  3. Rocknest commented on Nov 6, 2020

    @Rocknest
    Contributor

    I was thinking this behaviour is a bug

  4. alexnask commented on Nov 6, 2020

    @alexnask
    ContributorAuthor

    @dbandstra This affects the type of generic fns, and in turn typeInfo and typeName, yes.
    In this specific exzample @TypeOf(func) would become fn (u16, type, u32) void and the null values in typeInfo would get a concrete type value.

  5. cryptocode commented on Feb 8, 2021

    @cryptocode
    Contributor

    This would be great to get fixed, I have a use case where it's needed but am running into:

    // NOTE: replacing anytype with u64 makes it compile
    fn less(a: anytype, b: anytype) bool {
        return a < b;
    }
    
    fn mul(a: anytype, b: anytype) @TypeOf(a) {
        return a * b;
    }
    
    fn Something(comptime func: anytype) type {
        const RET = @typeInfo(@TypeOf(func)).Fn.return_type.?;  // <--- error: unable to unwrap null
        /// ....
        return RET;
    }
    
    pub fn main() void {
        const a = Something(less);
        const b = Something(mul);
    }
  6. modified the milestones: 0.8.0, 0.9.0 on May 19, 2021
  7. modified the milestones: 0.9.0, 0.10.0 on Nov 23, 2021
  8. modified the milestones: 0.10.0, 0.11.0 on Apr 16, 2022
  9. Vexu commented on Jan 13, 2023

    @Vexu
    Member

    Implemented in self-hosted:

    git grep "generic function types" test
    test/behavior/type_info.zig:test "type info: generic function types" {
    
  10. added
    accepted
    on Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedproposal

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions