#Implicit conversion without casting?

1 messages · Page 1 of 1 (latest)

spare pelican
#

Is it possible to do sn automatic implicit conversion without casting? I have a struct that is working basically as a custom int (int with some modifications to the value)

I have implicit and explicit operators setup.

What i have to do is: CustomInt value = (CustomInt)12;

What i want to do is CustomInt value = 12;

Is this possible?

dim lintel
#

What you're trying to do is called implicit conversion

spare pelican
#

Ah your right, I get those mixed up sometimes

#

Implicit conversion without casting?

#

Ok i fixed the name

hallow sable
#

provided you actually do have the implicit conversion operator for that type defined in your object it will work

spare pelican
#

Weird... I get an error asking if I forgot to cast

hallow sable
#

show the relevant code

dim lintel
#

Both the code that throws the error, the error itself, and the struct definition.

spare pelican
#

Alright, apologies ill have to come back in a bit as Im currently away from my PC.

#

Ill put the code here when im back

fathom cypress
#

what you describe is exactly how LayerMask in unity works

  public static implicit operator int(LayerMask mask) => mask.m_Mask;

  public static implicit operator LayerMask(int intVal)
  {
    LayerMask layerMask;
    layerMask.m_Mask = intVal;
    return layerMask;
  }
#

also many other things like the vector conversions

spare pelican
#

Looking at this i might already know the issue, im not at my computer right now but I think I marked one of my operators explicit which would explain why it wants me to cast

#

Ill wait till im able to confirm thats the issue before marking solved

#

Thank you for the comment !