vanille.
vanille.
CC#
Created by vanille. on 1/15/2024 in #help
Change function behavior depending on class' generic type.
Hello! I was wondering if there was a way to change the behavior of a function in a class with a generic type. I was originally looking into overloading but didn't have any luck. Is what I'm trying to do possible?
public class Lerpable<T>
{
private readonly T _value1;
private readonly T _value2;

public Lerpable<T> (T value1, T value2)
{
_value1 = value1
_value2 = value2
}

public T Lerp(float amount)
{
// Default behavior, I want to be able to change this depending on what T is.
return amount > 0.5 ? _value2 : _value1;
}
}
public class Lerpable<T>
{
private readonly T _value1;
private readonly T _value2;

public Lerpable<T> (T value1, T value2)
{
_value1 = value1
_value2 = value2
}

public T Lerp(float amount)
{
// Default behavior, I want to be able to change this depending on what T is.
return amount > 0.5 ? _value2 : _value1;
}
}
15 replies