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
7
votes
3 answers

Member with the same signature already defined with different type constraints

I have run across an issue with overloading methods that have different constraints that seems exclusive. That is my example: public class A { public void Do() where T : class { } public void Do() where T : struct { …
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
6
votes
3 answers

How do I constraint a generic type parameter to only accept nullable value types in C#?

The where T : struct constraint lets one to limit the domain of acceptable type parameters to the set of value types (as compared to the superset of types including both value and reference types) only but also seems to forbid nullable types…
Ivan
  • 63,011
  • 101
  • 250
  • 382
6
votes
4 answers

Defining generic interface type constraint for value and reference types

Im having some trouble getting this generic constraint to work. I have two interfaces below. I want to be able to constrain the ICommandHandlers TResult type to only use types that implement ICommandResult, but ICommandResult has its own…
Matt
  • 2,730
  • 4
  • 28
  • 35
6
votes
4 answers

how to use where for operators at generics class c#?

Hi I would like to have a class: class Matrix where T : // I don't know what to write here { T[][] elements; } I would like T to be addable and multiplyable by + and * operators
5
votes
4 answers

Delphi XE: Can I call virtual constructors with parameters from a classtype-constrained generic type without resigning to hacks?

I'm trying to build a generic ancestor for composite controls. The initial idea looked something like this: type TCompositeControl = class(TWinControl) private FControl1, FControl2: TControl; …
5
votes
2 answers

Generic constraints -- I'm not sure how to fix this situation with an either/or case

Basically I have the following: public static bool IsBetween(this T value, T a, T b) where T : IComparable { ... } public static bool IsBetween(this T value, T a, T b) where T : IComparable { ... } The problem is I can't…
myermian
  • 31,823
  • 24
  • 123
  • 215
5
votes
2 answers

How do I interpret the argument of method comparing(Function keyExtractor)?

Full signature of method: public static > Comparator comparing( Function keyExtractor) I'm learning lambda expressions and I have this piece of code that compare a List of…
Bogdan
  • 623
  • 5
  • 13
5
votes
1 answer

Generic constraint: Enforce type to have static function and constructor with parameters

I know you can write: class GenericClass where T : new() { } to enforce that T has an empty constructor. My Qs are : can you enforce that T has a constructor with a specific type of parameter? Like: class…
geth
  • 61
  • 2
5
votes
1 answer

Swift Generics: Constrain Type Parameter to Protocol

How do you specify that a generic type parameter can only be a protocol (or a protocol conforming to that protocol), and not a class conforming to that protocol? For example: import Foundation @objc protocol MyProtocol { var name: String { get…
Coder-256
  • 5,212
  • 2
  • 23
  • 51
5
votes
3 answers

TypeScript "extends" generic constraint

I am using the extends constraint in TypeScript, like so: class Animal {} class Lion extends Animal {} class Bear extends Animal {} class ZooKeeper { constructor(p: T = new Animal()) { } } new ZooKeeper(new…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
5
votes
1 answer

Why can't we satisfy F# static member constraints with type extensions?

I'd like to be able to extend types from other libraries with static methods to enable generic arithmetic. Take, for example, the newly minted SIMD-friendly fixed-size VectorN types from Microsoft. They define Zero, they define (+), they define (/),…
Sebastian Good
  • 6,310
  • 2
  • 33
  • 57
5
votes
3 answers

C# unbounded generic type as constrain

Is it possible to have a generic constraint which is an unbounded generic type? For example: public T DoSomething(T dictionary) where T : IDictionary<,> { ... } Edit: Just to explain the context, I want to constrain the usage of the method…
Amir Abiri
  • 8,847
  • 11
  • 41
  • 57
4
votes
4 answers

Need help to understand this C# generic class

I am learning Nhibernate 3.0. In one of the sample code examples, it creates an abstract base entity class: public abstract class Entity where T : Entity then, make the Customer entity inherit from the Entity base class: public class…
qinking126
  • 11,385
  • 25
  • 74
  • 124
4
votes
1 answer

Generic function to work on different structs with common members from external package?

I want to write a single function that can add certain fields to Firebase message structs. There are two different types of message, Message and MulticastMessage, which both contain Android and APNS fields of the same types, but the message types…
realh
  • 962
  • 2
  • 7
  • 22
4
votes
2 answers

Rust Async Borrow Lifetimes

I'm trying to make a helper that allows asynchronously chaining side effects, but I'm unable to get the generic bounds correct so that the compiler understands the output of the future outlives a reference used to build it. Playground Link The gist…
moatra
  • 565
  • 1
  • 4
  • 10
1
2
3
9 10