Questions tagged [generic-constraints]

A generic constraint will restrict the types that can be used for a specific generic. Use this tag for questions regarding generic constraints.

allow a programmer to restrict the types, which can be used for specific . This will also allow the compiler to ensure that specific object members exist. Therefore programmers can write more flexible code.

This feature is implemented in a range of languages including , , , , and .

136 questions
0
votes
3 answers

TypeScript - typesave mapping types of restricted tuple generic

In TypeScript this is not compiling: export interface Generic { } export interface Class { readonly prop: { [P in keyof T]: Generic } } Particularly, the Generic fails with Type 'T[P]' does not…
Reizo
  • 1,374
  • 12
  • 17
0
votes
0 answers

C# Generic parameters - is there any "nullable or not" constraint

I'm often building tools with generics and Expression> to target properties of the object. I keep struggling on a very stupid limitation of C# (but maybe I am ignorant of the proper solution). Let's say we have this object: class…
Mose
  • 1,781
  • 3
  • 16
  • 35
0
votes
1 answer

Can I have multiple class constraints without setting the constraint to a type 'class' in C#

I have this class, it's part of my specification pattern. Following this link public class SpecificationEvaluator where TEntity : BaseEntity and BaseEntity consists of just the id ex. public class BaseEntity { public int Id { get;…
chuckd
  • 13,460
  • 29
  • 152
  • 331
0
votes
0 answers

The covariant type is not used as a generic constraint for the interface methods, why? (C#)

The problem: interface ICovariant { // The following statement generates a compiler error // because you can use only contravariant or invariant types // in generic constraints. // void DoSomething() where T : R; } Why is…
0
votes
3 answers

Generics and Constraints (`Instance method '...' requires that 'T' conform to 'Decodable'`)

I have a generic struct allowing different types to be used. I do not want to constrain the whole struct to only Decodable items. What is the best way to fix the following error, where I try to only execute some code if T conforms to…
smat88dd
  • 2,258
  • 2
  • 25
  • 38
0
votes
2 answers

How Can I Write This Constraint?

The task is to write an alter query for general constraints. Specifically: "In Employee, if EmployeeLevel is NULL, then EmployeeSalary must also be NULL". If anyone could explain a correct solution, it would be much appreciated! ALTER TABLE…
0
votes
1 answer

Complex generic type constraint

I'm trying to write a function getDocument which would do the following: interface DocumentReference { get(): Promise; } interface ParentA { foo: DocumentReference; } interface ParentB { bar: DocumentReference; } //…
ldiqual
  • 15,015
  • 6
  • 52
  • 90
0
votes
1 answer

Multiple generic constraints with defaults relying on each other

This is my Typescript interface/class structure that I have: interface IBaseOptions { homeUrl: string; } abstract class BaseApp { constructor( private options: TOptions ) { …
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
0
votes
1 answer

Is there a way to avoid passing two generic parameter in my Extension method?

I have a class of type Installer where TModel has a constraint. I want to create an extension method with the type signature :\ public static void DoRepetitiveStuff(this Installer installer) where TModel : class,…
0
votes
0 answers

Why doesn't an AnyObject constraint on a generic parameter allow that generic parameter to be used as a constraint?

While playing around with the code in another SO post, I found this interesting behaviour. As we know, the type of a weak var needs to be a class type and the type used in a generic constraint needs to be a class type, or a protocol…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0
votes
1 answer

What is so special about IComparable<> as a Generic Constraint

Suppose I have a class Class Apple where T: IComparable{} Now while creating an object for Apple I'll do it like Apple obj = new Apple(); The above code will work. If I replace the same code with any other Generic Interface like…
srikar kulkarni
  • 630
  • 1
  • 6
  • 16
0
votes
0 answers

Typescript - Generic Constraints - not accepting object literal of matching type

Using typescript 3, in angular 7 framework. I'm trying to prepare my local Store array elements for firebase, using an object's Id property as it's key in a wrapping object, and vise versa when fetching from firestore. I've set up an interface that…
toms
  • 515
  • 6
  • 23
0
votes
1 answer

Swift - Differences between inheriting protocol and constraining Self to protocol

I have a protocol A: protocol A { } What are the differences between implementing protocol B like this: protocol B: A { } versus implementing it like this: protocol B where Self: A { } ? What can I do with one that I can't do with the other?…
jeremyabannister
  • 3,796
  • 3
  • 16
  • 25
0
votes
0 answers

Why is there Self Generic Type Constrained?

I'm reviewing some code written by some ex employees and found this public abstract class BaseClass where T : BaseClass, M where M : IDisposable { protected M PropertyName {get; private set} ... // T is never used in this…
Saad
  • 198
  • 2
  • 12
0
votes
2 answers

Is possible to combine Generic Constraint with operator overloading?

I wonder if with Generic Constraint (when) it is possible to allow this code ? And what is the right pattern to do this? public MyClass { public void MyMethod(T a, T b) { //some code var result = a
Christophe Debove
  • 6,088
  • 20
  • 73
  • 124