Page MenuHomeFeedback Tracker

Support for type-constrained templates
Closed, DuplicatePublic

Description

It's currently not possible to restrict a template to be only instantiated with a specific class or its subclasses.
Possible syntax for such a feature could be:

class Container<Class T extends Element> {};    // Java-like
class Container<Class T> where T : Element {};  // C#-like

Details

Severity
Feature
Resolution
Open
Reproducibility
N/A
Operating System
Windows 11 x64
Category
General

Event Timeline

ookexoo created this task.Wed, Jul 2, 11:30 PM

I just realized that there is technically already a syntax for it the code base:

class SCR_Sorting<Class T, SCR_SortCompare TCompare>
{
...

but it doesn't seem to be enforced, as the example below still compiles:

class WrongClass
{
	static int Compare(Managed left, Managed right);
}

void RunTest()
{
	SCR_Sorting<Managed,WrongClass> sorting = new SCR_Sorting<Managed, WrongClass>();
}
Geez changed the task status from New to Feedback.Thu, Jul 3, 12:25 PM

Turns out there is already a ticket for it: T166418