Compilation error after added Lazy Values (in the LazyValues file)

The error is pointing to this funtion in LazyValue.cs with the error "user defined conversion must convert to or from the enclosing type. any clues? Any other info needed? I’ve made the changes Sam did in the lecture. It’s possible I have a typo… those little guys can escape my aging eyes…

       public static implicit operator float(LazyValue<float> v)
        {
            throw new NotImplementedException();
        }

This implicit conversion operator is not in my LazyValue class. And, as you have discovered, it won’t work. The error states that the conversion must be to/from the enclosing type. You are trying to convert from LazyValue<float>, but the enclosing type is LazyValue<T>; not the same

There are some simple ways around it - like wrapping the wrapper - but this implicit conversion may cause confusion later down the line when you see this float and it’s not actually a float

But here is an example

public class LazyFloat : LazyValue<float>
{
    public LazyFloat(InitializerDelegate initializer) : base(initializer) { }

    public static implicit operator float(LazyFloat lazyFloat)
    {
        return lazyFloat.value;
    }
}

You can do this for all your common types

I really wish I understood this. It doesn’t appear others are having this problem, so why specifically me? It must be something in my code, right?

How would I use your solution above? Would I declare the variable as type LazyFloat rather than LazyValue?

Well, like I said, I don’t have the implicit operator. I also watched the lecture again and don’t see Sam ever adding it (I only watched the one this post is linked to). So from my side, this is why I don’t have that issue. Because I don’t have that code.

My solution is just a wrapper around the LazyValue wrapper to overcome the error you received by changing the ‘enclosing type’ to a lazy value that accepts float. Yes, you would use LazyFloat instead of LazyValue<float> but it will (should, at least) cast to LazyValue<float> because technically it’s still the same thing.


Edit
Just went to the source repository and the LazyValue in there does not have the implicit operator either.

See here: LazyValue.cs

I suspect your IDE decided to ‘help you out’ and created the operator. Even if you could do that, you would have received an error because the implementation is to throw a NotImplementedException. I think you can safely remove the function

well, yep. If I just take that operator out, everything compiles. I guess the first clue would have been the fact that there was no function body - just the “throw new NotImplementedException”. Helpful, VS code… fooey. Thanks for the help!

Yes, it definitely looks like VS Code tried to “help”… (in all fairness, I use Rider, and sometimes it tries to help as well).

There’s no reason to edit the LazyValue.cs file at all… You declare an instance of the LazyValue with the type that T is supposed to represent, and the issue may be that you were trying to assign a LazyValue<float>.value to an integer (which would yield a no implicit conversion exists error, as float can’t convert to int directly.

I was only looking in that file because Unity pointed to that as the problem line. Weird. After I deleted the code that VS Code added, it compiled without error. I can’t find anywhere that a LazyValue.value was being assigned to an int (or any other incorrect type), and it works fine now. Just weird.

We originally had float values which we changed to LazyValue<float>. It is possible that somewhere in the code the new ‘lazy float’ was being assigned to a standard float before all the changes were made. VS Code, Rider, and Visual Studio all have this ‘helpful’ feature that detects this and offers to implement an implicit conversion override. Perhaps you inadvertently accepted this offer and it created the function. Happens to me all the time. I actually use the ‘helpful’ features a lot and I got used to specific offers being in a specific order and pick the ones I want without looking. Sometimes there’s an unexpected one before the one I want and I end up picking the wrong one. I’ve had nightmares from picking an option and seeing nothing happen. Makes me sweat a little, not knowing what could possibly have changed and not knowing where this happened

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms