Constraints can be associated with a type parameter of a generic. Constraints declare items that must be supported by any concrete type passed to that parameter in a construction of the generic type.
Questions tagged [type-constraints]
336 questions
325
votes
11 answers
What does "where T : class, new()" mean?
Can you please explain to me what where T : class, new() means in the following line of code?
void Add(T item) where T : class, new();
![](../../users/profiles/401432.webp)
Rawhi
- 5,367
- 6
- 32
- 53
202
votes
4 answers
What do <:<, <%<, and =:= mean in Scala 2.8, and where are they documented?
I can see in the API docs for Predef that they're subclasses of a generic function type (From) => To, but that's all it says. Um, what? Maybe there's documentation somewhere, but search engines don't handle "names" like "<:<" very well, so I…
![](../../users/profiles/33775.webp)
Jeff
- 12,441
- 15
- 46
- 58
119
votes
2 answers
C# generic "where constraint" with "any generic type" definition?
Let me give example:
I have some generic class/interface definition:
interface IGenericCar< T > {...}
I have another class/interface that I want to relate with class above, for example:
interface IGarrage< TCar > : where TCar: IGenericCar< (**any…
![](../../users/profiles/186822.webp)
Nenad
- 19,511
- 8
- 58
- 80
83
votes
5 answers
How to define generic type limit to primitive types?
I have the following method with generic type:
T GetValue();
I would like to limit T to primitive types such as int, string, float but not class type. I know I can define generic for class type like this:
C GetObject() where C: class;
I am…
![](../../users/profiles/62776.webp)
David.Chu.ca
- 33,436
- 60
- 141
- 188
53
votes
5 answers
Why does a generic type constraint result in a no implicit reference conversion error?
I have created a couple of interfaces and generic classes for working with agenda appointments:
interface IAppointment where T : IAppointmentProperties
{
T Properties { get; set; }
}
interface IAppointmentEntry where T :…
![](../../users/profiles/2544114.webp)
Rens
- 532
- 1
- 4
- 7
33
votes
2 answers
How do I specify multiple constraints on a generic type in C#?
What is the syntax for placing constraints on multiple types? The basic example:
class Animal where SpeciesType : Species
I would like to place constraints on both types in the following definition such that SpeciesType must inherit…
![](../../users/profiles/327.webp)
Luke
- 17,750
- 24
- 81
- 108
23
votes
4 answers
Why am I getting a generic constraint violation at runtime?
I'm getting the following exception while trying to create a new instance of a class that heavily relies on generics:
new TestServer(8888);
System.TypeLoadException
GenericArguments[0], 'TOutPacket', on …
![](../../users/profiles/154502.webp)
Lazlo
- 7,888
- 13
- 69
- 113
21
votes
3 answers
C# generic methods, type parameters in new() constructor constraint
Is there a way to create a Generic Method that uses the new() constructor constraint to require classes with constructors of specific types?
For Example:
I have the following code:
public T MyGenericMethod(MyClass c) where T : class
{
if…
![](../../users/profiles/382059.webp)
Meryovi
- 5,804
- 4
- 37
- 63
21
votes
3 answers
Why does this violate the type constraint?
I'm trying to customise ASP.NET Identity 3 so that it uses integer keys:
public class ApplicationUserLogin : IdentityUserLogin { }
public class ApplicationUserRole : IdentityUserRole { }
public class ApplicationUserClaim :…
![](../../users/profiles/586012.webp)
James
- 6,911
- 9
- 40
- 80
20
votes
1 answer
Debugging compile time performance issues caused by GHC's constraint solver
Haskell has many great tools for debugging run time performance issues, but what tools/rules of thumb exist for debugging compile time performance issues?
Specifically, the constraint solver in some of my code is taking forever (anywhere from 1-2…
![](../../users/profiles/1241368.webp)
Mike Izbicki
- 6,126
- 1
- 19
- 45
19
votes
2 answers
Reflexive type parameter constraints: X where T : X ‒ any simpler alternatives?
Every so often I am making a simple interface more complicated by adding a self-referencing ("reflexive") type parameter constraint to it. For example, I might turn this:
interface ICloneable
{
ICloneable Clone();
}
class Sheep : ICloneable
{
…
![](../../users/profiles/240733.webp)
stakx - no longer contributing
- 77,057
- 17
- 151
- 248
16
votes
2 answers
How do I translate a `where T : U` generic type parameter constraint from C# to F#?
F# is giving me some trouble with its type inference rules. I'm writing a simple computation builder but can't get my generic type variable constraints right.
The code that I would want looks as follows in C#:
class FinallyBuilder
{
…
![](../../users/profiles/240733.webp)
stakx - no longer contributing
- 77,057
- 17
- 151
- 248
16
votes
1 answer
The case of the disappearing constraint: Oddities of a higher-rank type
All the experiments described below were done with GHC 8.0.1.
This question is a follow-up to RankNTypes with type aliases confusion. The issue there boiled down to the types of functions like this one...
{-# LANGUAGE RankNTypes #-}
sleight1 :: a…
![](../../users/profiles/2751851.webp)
duplode
- 31,361
- 7
- 69
- 130
16
votes
1 answer
Is it possible to introduce additional type variables into a superclass-constraint?
When dealing with type families, it is often handy to use equality constraints to avoid having to repeat some type-function's name in a signature:
class Foo f where
type BulkyAssociatedType f :: *
foo :: BulkyAssociatedType f -> f
...
bar ::…
![](../../users/profiles/745903.webp)
leftaroundabout
- 101,764
- 3
- 156
- 291
15
votes
3 answers
Why Do I need to redeclare type constraint in generic subclass
Recently I tried to create a generic subclass by implementing a generic interface.
public interface IModule where T : DataBean { ..... }
public class Module : IModule where T : DataBean { .... }
It seems I can't rely on any of T's…
![](../../users/profiles/59137.webp)
Paul
- 1,697
- 2
- 11
- 21