#Comptime inference with expectEqual

1 messages · Page 1 of 1 (latest)

agile tide
#

Sometimes std.testing.expectEqual surprises me with the error that a type cannot be comptime inferred, when I'm using just a bunch of fields selections on structs. I understand why this method errors when it cannot infer the type at comptime, but I don't understand why the compiler cannot infer this type.

For example I was doing this:

try std.testing.expectEqual(0, hittable.bbox.x.min);

Hittable, bbox, x, all have a static type defined for their property, and the type of min is f64.

elfin mica
#

you need to reverse your testing I think

#

it should be

try std.testing.expectEqual(hittable.bbox.x.min, 0);
#

I'm not sure but I think it's something about this functions infering the type of the equality based on the first argument's type and since 0 is a comptime_int it probably try to evaluate the other as comptime too

agile tide
#

does that mean the documentation on this method is incorrect? Or am I just misinterpreting this, really seems to me the first argument should be what you expect the value to be?

elfin mica
#

I think it reads expected of anytype, actual of the the same type as expected type

agile tide
#

ah I see, so the type inference goes from expected -> actual. So if you pass expected = 0, the compiler cannot infer what type that is (int or float etc)?

elfin mica
#

so I think this is why you get an issue It infered your second argument of being of type comptime_int which your second argument is not

#

I fell into that many times lol

elfin mica
#

This is also one of the reason why when you forget a try the compiler spills out garbage not recognizing your type too i think

#

which at first made me really confused

agile tide
#

I have noticed this difference between comptime_int/float and 'normal' ints/floats. But I don't understand why this is required, other types don't have this distinction between a comptime and runtime variant?

elfin mica
#

do you have specific types in mind ? I'm not sure I understand what you mean ?

#

If I can take a wild guess one of the reasons why they have comptime_variant is that they are used to distinguish between the normal one when you are doing comptime meta programming, so that if you accidently call a comptime function at runtime it will fail maybe ?

void mesa
elfin mica
#

also one of the goal of the language is to be very explicit, in this example it's hard to look at i and f at a glance and know exactly what type they should be

var i = 0 //evaluates to comptime_int
var f = 1.0 // evaluates to comptime_float
var j : i32 = 0 //evaluates to comptime_int and get inferred to be of type i32
#

like is i an u1 ? is it an i128 ? don't really know, same for f is it a f32 ? a f64 ? it's hard to know whereas other types might be easier to know just by looking at it for example

var str = "this is a string";

it can be infered because it can't be a 100 different things maybe ?

agile tide
#

If I can take a wild guess one of the reasons why they have comptime_variant is that they are used to distinguish between the normal one when you are doing comptime meta programming, so that if you accidently call a comptime function at runtime it will fail maybe ?
Other types are prefixed with the comptime keyword (as far as I have seen), as opposed to a different type. There must be some reason for this distinction 🤔. I haven't done any metaprogramming so far though, so maybe I should try that to understand these differences.

elfin mica
#

or maybe it's to prevent you from using variables for initialization of comptime object ? since a variable is a runtime thing ? not sure too I haven't delved deep into the matter, I think you should search on medium for an article about comptime I remember reading one that was pretty clear

#
In other words, once you make your function comptime by making a parameter comptime, you cannot pass non-comptime parameters such as a command-line argument.
#

I think this might explain why ?