#unable to evaluate constant expression

1 messages · Page 1 of 1 (latest)

vast herald
#
const global = "from us";

fn bar(a: []const u8, b: []const u8) []const u8{
  
 return a ++ b;
}

fn hello(a: []const u8, b: []const u8) []const u8 {
  var barred = bar(a, b);
  std.debug.print("THE BARRED {}", .{barred});
  return "Hello" ++ barred ++ " " ++ global;
}

pub fn main() !void {
    // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
    var result = hello("friends", "family");
    std.debug.print("SOME STUFF{}", .{result});
}

this code is giving me the following error

./src/main.zig:14:21: error: unable to evaluate constant expression
  return "Hello" ++ barred ++ " " ++ global;
                    ^
./src/main.zig:7:9: error: unable to evaluate constant expression
 return a ++ b;
vast pulsar
#

The ++ concatenation operator only works on comptime-known values, and barred is a runtime value. If you need to concatenate strings that are runtime values, you can use sdlib functions to join them

vast herald
#

or could i make my args comptime?

#

and i guess if not, im not sure if you can rec any specific stdlib functions... was just looking around github and not seeing any for concatenation

vast pulsar
vast herald
#

got it to work comptime... but I'd love to be able to print it out runtime too

vast pulsar
#

If you want to use comptime values, here is a fixed version of your snippet

const std = @import("std");
const global = "from us";

fn bar(comptime a: []const u8, comptime b: []const u8) []const u8 {
    return a ++ b;
}

fn hello(comptime a: []const u8, comptime b: []const u8) []const u8 {
    const barred = comptime bar(a, b);
    std.debug.print("THE BARRED {s}", .{barred});
    return "Hello" ++ barred ++ " " ++ global;
}

pub fn main() !void {
    var result = hello("friends", "family");
    std.debug.print("SOME STUFF {s}", .{result});
}
vast herald
#
const global = "from us";

fn bar(comptime a: []const u8, comptime b: []const u8) []const u8{
  
 return a ++ b;
}

fn hello(comptime a: []const u8, comptime b: []const u8) []const u8 {
  var barred = comptime bar(a, b);
  // std.debug.print("THE BARRED {}", .{barred});
  return "Hello" ++ barred ++ " " ++ global;
}

pub fn main() !void {
    // var foo: []const u8 = "something"
    comptime {
      var result = hello("friends", "family");
      @compileLog(result);
    }
    std.debug.print(result);
    // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
}
vast pulsar
#

haha, same time

vast herald
#

lol

#

ah but you're able to still use std.debug because you're just making the vars comptime w/o using a comptime block?

vast herald
#

yeah I seem to be struggling to print the output at runtime

vast pulsar
vast herald
#
const global = "from us";

fn bar(comptime a: []const u8, comptime b: []const u8) []const u8{
 return a ++ b;
}

fn hello(comptime a: []const u8, comptime b: []const u8) []const u8 {
  var barred = comptime bar(a, b);
  return "Hello" ++ barred ++ " " ++ global;
}

pub fn main() !void {
    comptime {
     var result = hello("friends", "family");
     std.debug.print("THE RESULT {}", .{result});
    }
}```
#

It yells at me when I do that

#
/Users/interpretations/Downloads/zig-macos-aarch64-0.11.0-dev.514+4be1bb4aa/lib/std/Thread/Mutex.zig:111:18: error: unable to evaluate constant expression
        os.darwin.os_unfair_lock_lock(&self.oul);
                 ^
/Users/interpretations/Downloads/zig-macos-aarch64-0.11.0-dev.514+4be1bb4aa/lib/std/Thread/Mutex.zig:45:19: note: called from here
    self.impl.lock();
                  ^
/Users/interpretations/Downloads/zig-macos-aarch64-0.11.0-dev.514+4be1bb4aa/lib/std/debug.zig:90:22: note: called from here
    stderr_mutex.lock();
                     ^
./src/main.zig:20:21: note: called from here
     std.debug.print("THE RESULT {}", .{result});
                    ^
./src/main.zig:16:21: note: called from here
pub fn main() !void {
                    ^
#

even if i remove the comptime block in main

#

go tit

#

got it*

#
const global = "from us";

fn bar(comptime a: []const u8, comptime b: []const u8) []const u8{
 return a ++ b;
}

fn hello(comptime a: []const u8, comptime b: []const u8) []const u8 {
  var barred = comptime bar(a, b);
  return "Hello" ++ barred ++ " " ++ global;
}

pub fn main() !void {
     var result = comptime hello("friends", "family");
     std.debug.print("THE RESULT {s}", .{result});
}
#

seemed the trick was comptime before the final func execution

vast pulsar
#

Did the version I pasted not work for you? What version of zig are you using?

#

Oh, I also made barred a const instead of var. Anyway

vast herald
#

yours works

#

I'm trying to recall what the difference was in mine that felt significant to me

severe fiber
#

To be clear: ++ is std.mem.concat, but with comptime-known-sized values, or slices that are comptime-known.