Replies: 4 comments 16 replies
-
What would be an example of code you would write if this was allowed? |
Beta Was this translation helpful? Give feedback.
-
public class UpCastableCommon: UpCastableBase
{
}
public class UpCastableDerivedTest<R>: OriUpCastBase<UpCastableCommon, R>
{
}
public abstract class OriUpCastBase<TCommon, TReturn>
where TCommon: UpCastableBase
{
public TReturn Return { get; private set; }
public TCommon Common { get; private set; }
public static implicit operator TReturn(OriUpCastBase<TCommon, TReturn> incoming)
{
return incoming.Return;
}
public static implicit operator TCommon(OriUpCastBase<TCommon, TReturn> incoming)
{
return incoming.Common;
}
}
public static class OriUpCastable<TCommon, TReturnWrapper, TReturn>
where TCommon: UpCastableBase
where TReturnWrapper: OriUpCastBase<TCommon, TReturn>
{
public static TReturnWrapper UpCast(TCommon incomingWrapper)
{
return default;
}
}
UpCastableCommon test1 = new UpCastableCommon();
UpCastableDerivedTest<IList> test2 = new UpCastableDerivedTest<IList>();
List<string> test10 = OriUpCastable<UpCastableCommon,UpCastableDerivedTest<List<string>>, List<string>>.UpCast(test1);
IList test10 = OriUpCastable<UpCastableCommon,UpCastableDerivedTest<IList>, IList>.UpCast(test1); // this gives an invalid cast error as described by https://github.com/dotnet/roslyn/issues/47399
OriSettings test3 = OriUpCastable<UpCastableCommon,UpCastableDerivedTest<OriSettings>, OriSettings>.UpCast(test2); this allows passing a single object around while storing data in properties that are dependent on the type its cast to through the same object, not fully fleshed out but I wanted to get the framework down first and it works, with this minor error (warning really) |
Beta Was this translation helpful? Give feedback.
-
As a general rule, if you all you want to do is to make currently legal code illegal, roslyn analyzers are a great way to do that, and don't require language changes. Language changes would only be considered for such things if by making certain code illegal, that allows us to do something new which is currently illegal, or if the use case is extremely common. |
Beta Was this translation helpful? Give feedback.
-
Another very strong use case: public class NavigationCarrier_Basic<O>: UpCastableCommon, INavigationCarrier_Basic<O>
{
NavigationCarrier_Data<O> PreviousObject { get; set; }
NavigationCarrier_Data<O> CurrentObject { get; set; }
}
public class NavigationCarrier_Basic<O, R>: OriUpCastBase<NavigationCarrier_Basic<O>, R>
{
R Result { get; }
}
public abstract class OriGenericVisitor_TypeQualifier<O, N, R>: OriGenericVisitorBase<O, N>
where N: NavigationCarrier_Basic<O, R>, new()
{
protected N Navigation(N){}
protected bool Process(O incomingObjectToProcess)
{
...
N newN = new N();
NavigationCarrier_Basic<O> castN = newN;
castN.PreviousObject = new NavigationCarrier_Data<O>()
{
Data = incomingPreviousObject
};
castN.CurrentObject = new NavigationCarrier_Data<O>()
{
Data = incomingNextObject
};
N reforgedN = OriUpCastable<NavigationCarrier_Basic<O>, N, R>.UpCast(castN);
NavigationCarrier_Basic<O, R> navigationResult = this.Navigation(reforgedN);
...
}
} implementing something like this using reflection is doable, but this is done at compiler time instead of runtime and is far more readable. id argue next gen reflection. it would be nice if the intellisense was capable of doing something like this N newN = new N(){
PreviousObject = new NavigationCarrier_Data<O>()
{
Data = incomingPreviousObject
};
CurrentObject = new NavigationCarrier_Data<O>()
{
Data = incomingNextObject
};
}; but its not |
Beta Was this translation helpful? Give feedback.
-
Given this issue: dotnet/roslyn#47399
there should be a way to limit a generic from ever being an interface?
Beta Was this translation helpful? Give feedback.
All reactions