#[solved] How to join string++char++string at comptime?

1 messages · Page 1 of 1 (latest)

nimble lance
#
some.name ++ '_' ++ field.name,
```Currently trying this, but does not work
Also tried `std.fmt.allocPrint`, but it doesn't work at compiletime it seems
#
  /// @descr Action to take when converting an enum field with `zstd.meta.enums.from(val, ToType, .{.prefix= OPTION})`
  ///  - none : Will act as if there were no prefixes
  ///  - add  : Will add a prefix to `val.fieldName` before searching for its field inside the {@arg To} list of fields.
  ///  - rmv  : Will remove a prefix to `val.fieldName` before searching for its field inside the {@arg To} list of fields.
  const ConversionPrefix = enum { none, add, rmv };
  /// @descr
  ///  Returns {@arg val} converted to the target type {@arg To}
  ///  The option {@arg args.prefix} (default: none) will dictate whether the SomeT_ prefix will be added, removed or ignored.
  ///  @note {@arg val} must be contained in {@arg To}
  pub fn from (val :anytype, To :type, args :struct {
      prefix :ConversionPrefix= .none,
    }) To {
    const info = @typeInfo(@TypeOf(val)).Enum;
    const id   = @intFromEnum(val);
    inline for (info.fields) | field | {
      const name = switch (args.prefix) {
        .none => field.name,
        .add  => meta.types.name(To)++.{enums.Separator}++field.name,
        .rmv  => enums.fieldName(field.name),
      };
      if (field.value == id) return @field(To, name);
    }
    unreachable;
  }
```Code for context. Just realized the problem might be somewhere else... related to getting the name 🤔
#

It's really hard to chop it down into bite-sized pieces of code, so this is the file

trail light
#

Not std.fmt.allocPrint but std.fmt.comptimePrint

nimble lance
#
1838:89: error: unable to resolve comptime value
pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
                                                                                        ^~~~
/home/arch/.zig/lib/std/fmt.zig:1838:89: note: argument to function being called at comptime must be comptime-known
src/lib/zstd/zstd/meta.zig:118:39: note: called from here
        .add  => std.fmt.comptimePrint("{s}{c}{s}", .{meta.types.name(To), enums.Separator, field.name}),
#

Like I said, the problm might lie somewhere else

gaunt canyon
#

and what is enums.Seperator? is that comptime known

prisma field
#

some.name ++ "_" ++ field.name

#

++ concatenates arrays (well, or slices or tuples), so it needs an array [1]u8{'_'} rather than a single integer value '_', and a shorthand for that is "_" (well, that gives you a pointer-to-array, but ++ treats that identically)

gaunt canyon
#

oh lol didnt notice that, good catch

nimble lance
#

it wasn't the separator failing, it was meta.types.name(To)

#
      const name = switch (args.prefix) {
        .none => comptime field.name,
        .add  => comptime meta.types.name(To) ++ enums.Separator.str ++ field.name,
        .rmv  => comptime enums.fieldName(field.name),
      }
```This got me past that error. Added `comptime` to all three branches
#
  pub fn from (val :anytype, To :type, args :struct {
      prefix :ConversionPrefix= .none,
    }) To {
    const info = @typeInfo(@TypeOf(val)).Enum;
    const id   = @intFromEnum(val);
    inline for (info.fields) | field | {
      const name = switch (args.prefix) {
        .none => comptime field.name,
        .add  => comptime meta.types.name(To) ++ enums.Separator.str ++ field.name,
        .rmv  => comptime enums.fieldName(field.name),
      };
      if (field.value == id) return @field(To, name);
    }
    unreachable;
  }
meta.zig:123:48: error: unable to resolve comptime value
      if (field.value == id) return @field(To, name);
                                               ^~~~
src/lib/zstd/zstd/meta.zig:123:48: note: field name must be comptime-known
referenced by:
    test.4321: src/tests.zig:33:52

Oh well, but now I'm stuck here in the next line again 😦

shrewd stream
#

to know name at comptime you need to know args.prefix at comptime which requires comptime args: struct { prefix: ConversionPrefix = .none }

nimble lance
#

just did that when trial/erroring, yep. was just going to post it

#

lol, what? 😵‍💫

meta.zig:123:48: error: enum 'lib.zstd.zstd.meta.enums.join(&.{ minim.rules.Tk.Base, minim.rules.Tk.Wht, minim.rules.Tk.Sp, minim.rules.Tk.Kw, minim.rules.Tk.Op }[0..5],.{ .prefix = true })' has no member named 'prefix = true })_space'
      if (field.value == id) return @field(To, name);
                                               ^~~~
src/lib/zstd/zstd/meta.zig:61:12: note: enum declared here
    return @Type(.{.Enum= .{
           ^~~~~
referenced by:
    test.4321: src/tests.zig:34:52
#

has no member named 'prefix = true })_space'