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
1 answer

Is there a practical difference between a type constraint on a generic type directly vs using a 'where' clause?

Is there any practical difference in capabilities between these two functions? func testX(value:T) where T:StringProtocol { // Do something } func testY(value:T){ // Do something } They both seem to compile and run…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
0
votes
0 answers

Inheritance with generics and overloading a method

I have a class with the following method: public abstract class TypedMessage{ public TypedMessage setBody(byte[] body) { this.body = body; return this; } In a subclass of TypedMessage, I want to overload this method.…
user489041
  • 27,916
  • 55
  • 135
  • 204
0
votes
1 answer

generic constraint to restrict collection to have same type of derived objects

I want to restrict Collection to have have only same type of class using generic. public class InvoiceLine where T : TransactionLine ,new() { private readonly IList _transactionLines = new List(); …
jjj
  • 1,136
  • 3
  • 18
  • 28
0
votes
2 answers

Generics with type constraints based on static members

I'm trying to use generics with constraints that don't appear to be supported, and I'm wondering if there is a clean work-around. The problem with my initial attempt hinges on the fact that you cannot have static members in interfaces, and…
Hatchling
  • 191
  • 1
  • 7
0
votes
1 answer

How can I filter, or have multiple options, for a generic function?

I have a function that is taking in a value, which I then send to NSUserDefaults to store in its Property List. func store(value: T, key: String) -> Bool { // send key, value to NSUserDefaults } How can I set up generic constraints so that the…
Steals
  • 117
  • 1
  • 8
0
votes
2 answers

Swift pattern matching

In the code below: protocol Serializable { } struct Owner: Serializable { var name: String } struct Car: Serializable { var owners: [Serializable] } let car = Car(owners: [Owner(name: "John"), Owner(name: "Mike")]) Mirror(reflecting:…
Sinisa Drpa
  • 887
  • 1
  • 5
  • 16
0
votes
4 answers

C# implement interface on "where TEntity : class"

I'm relatively new to .NET and I've stumbled on this particular issue: while following a tutorial on repository pattern, the definition of the class goes like this: public class GenericRepository where TEntity : class { ... That being…
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
0
votes
2 answers

Any way to constrain a generic interface to the type that implements it?

public interface ICloneable { T Clone(); } public Foo: ICloneable { public Foo Clone() { //blah } } Is there any way to constrain T to the type that implements the interface? (Foo in this case). It would be nice to…
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
0
votes
3 answers

Is possible to constraint a generic type parameter to whatever another generic type parameter accepts?

For example: class ClassA where TA: T1, T2, T3, T4 ... { } class ClassB where TB: whatever ClassA.TA accepts { ClassA MyA; } I don't want to copy ClassA's constraint to ClassB because of SSoT and DRY principles.
Daniel Santos
  • 1,451
  • 15
  • 38
0
votes
1 answer

Instantiating a type-constrained generic class without subclassing it

Background [Skip to Question if you're not interested in the background] I stumbled across this generic class definition when reading the other day, and it stumped me for some time: public abstract class Entity where T : Entity I was puzzled…
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
0
votes
1 answer

C# - change "where" restriction through inheritance

let's say I have an generic data access layer, that is supposed to support different implementations. To realize this I have an base interface IPeristenceService that defines all methods to access data inside the persistence tier. The interface…
Carsten
  • 11,287
  • 7
  • 39
  • 62
-1
votes
1 answer

How do you access a common property of generic type inside a method in C# (using constraints?)

I have the same method that repeats in my project for different classes: public Task DoSomething(Class1 class) { // do stuff var commonProperty = class.commonProperty // do stuff } And in a different service public Task…
-1
votes
1 answer

How to apply generic constraint to accept multi level of inheritance C#

I am looking for a way to support multi level of inheritance for generic type constraint. Common generic interface public interface ICommon { T OrignalData {get;set;} string ChangeJson {get;set;} T Merged {get;set;} void…
Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58
-2
votes
1 answer

C# method multiple constraints with "new"

I'm trying to define a method that has multiple constraints where the T type is able to be used as a constructor: private void GetData(string url, string token1, string token2, Action bulkInsert, string…
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
-2
votes
2 answers

Can I constraint a generic type parameter to only accept types implementng a particular generic interface and access the IF type parameters in C#?

What I seek to do is that define a generic type ClassB that would accept the TA type parameter to be assigned any type implementing a generic interface IInterfaceA but in whatever a way [the interface definition allows], regardless the type…
Ivan
  • 63,011
  • 101
  • 250
  • 382
1 2 3
9
10