Skip to content

Higher-Rank Types fail with "use of undeclared identifier" #4828

Open

Description

@s5bug

Consider the function:

pub fn foo(bar: fn (comptime A: type, _: A) A) []u8 {
  return bar([]u8, "Hello World!");
}

The latter two uses of A will fail with use of undeclared identifier, even though they're perfectly valid to use. A workaround verifies this:

fn foo_bar_type(comptime A: type, _: A) A {}

pub fn foo(bar: @TypeOf(foo_bar_type)) [] u8 {
  return bar([]u8, "Hello World!");
}

However, said workaround is not able to capture types from previous parameters:

pub fn Monad(
  comptime M: fn (comptime _: type) type,
  pure0: fn (comptime A: type, _: A) M(A), // How to capture M here?
  flat_map0: fn (comptime A: type, comptime B: type, _: M(A), _: fn (_: A) M(B)) M(B)
) type {
  return struct {
    pub const pure = pure0;
    pub const flat_map = flat_map0;
  };
}

(There's some pseudo-ish code here for testing my usecase)

Activity

  1. added this to the 0.7.0 milestone on Mar 28, 2020
  2. tadeokondrak commented on Sep 17, 2020

    @tadeokondrak
    Contributor

    Note that using @TypeOf on function definitions works:

    pub fn Monad(
        comptime M: @TypeOf(struct {
            fn anon(comptime _: type) type {}
        }.anon),
        pure0: @TypeOf(struct {
            fn anon(comptime A: type, _: A) M(A) {}
        }.anon),
        flat_map0: @TypeOf(struct {
            fn anon(comptime A: type, comptime B: type, _: M(A), _: fn (_: A) M(B)) M(B) {}
        }.anon),
    ) type {
        return struct {
            pub const pure = pure0;
            pub const flat_map = flat_map0;
        };
    }

    Edit: ...but doesn't seem to actually type check the functions against the types correctly.

  3. modified the milestones: 0.7.0, 0.8.0 on Oct 17, 2020
  4. modified the milestones: 0.8.0, 0.9.0 on Nov 6, 2020
  5. added
    use case
    on Mar 21, 2021
  6. modified the milestones: 0.9.0, 0.10.0 on May 19, 2021
  7. MVPuccino commented on Aug 12, 2025

    @MVPuccino

    Any updates on this? I've been trying to implement an interface akin to GenericReader/GenericWriter but the lack of "Higher-Rank Types" is quite the road-blocker.

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

    use case

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions