lambda parameter incorrectly resolving to int

I’ve got the following weird situation. I have a 2d array class, and I have an extension function that visits the elements for writing as follows:

public static void VisitW<T>(this Array2D<T> ar, System.Func<Vector2Int, T> func, RectInt r = default ) { Vector2Int v = default; var full = r.Equals(default); var y0 = full ? 0 : r.min.y; var y1 = full ? ar.Height : r.max.y; var x0 = full ? 0 : r.min.x; var x1 = full ? ar.Width : r.max.x; for (v.y = y0; v.y < y1; ++v.y) for (v.x = x0; v.x < x1; ++v.x) ar.Set(v, func(v)); } 

Now some client code does this:

var ar = new Array2D<byte>(512, 512); ar.VisitW(idx_ => { Vector2 v = idx_; return math.Perlin.Noise(v * 10.0f); }); 

The compiler complains that idx_ is an int, and it won’t compile. Mind you, this issue occurs only rarely, as I have a lot of VisitW() calls through the code, and I do not have any other VisitW overload.

The error message is “Cannot implicitly convert type “int” to Vector2″. The issue is that idx_ is not an int, it’s a Vector2Int!

Any idea what can be wrong? I’m at a loss…

EDIT: Aaaaaand I found the answer seconds after I posted. In my lambda I was returning a float while it should have been a byte. So, instead of pointing out that my return type is incorrect, the compiler decides that the function should have had an int as an argument. Pfft.

submitted by /u/aotdev
[link] [comments]

Leave a Reply