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

Rust equivalent to Swift extension methods with equal generic parameter constraint?

In Swift, I can add method to a generic type with parameter equality constraint. extension Optional where Wrapped == String { // Available only for `Optional` type. func sample1() { ... } } How to do this in Rust? Update This…
eonil
  • 83,476
  • 81
  • 317
  • 516
3
votes
4 answers

How to call a method with a generic constraint using a type parameter without the constraint?

Let's say I have a method: public void ExampleMethod(T x) where T : struct // or new() { // example usage, similar to what's actually happening var z = (T)Enum.Parse(typeof(T), privateFieldOfTypeString); // do something depending on…
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
3
votes
3 answers

How to constrain nested generic types of a generic method

I'm trying to create a method which returns data from the database based on the given generic type. The interface: (this definition compiles) public interface IOrderPosition where TOrder : IOrder where TArtile :…
Holly
  • 1,305
  • 2
  • 15
  • 30
3
votes
2 answers

C# - Type Constraints and Limitations, any workaround to keep Type Safety?

I have a pretty common scenario about the limitations of Generic Type Constraint that would required another Generic to be defined. It has already been discussed (Eric Lippert himself and others) but so far I haven't seen a general guideline or…
Natalie Perret
  • 8,013
  • 12
  • 66
  • 129
3
votes
0 answers

is there any way to use generic class/method with 2 type that will not be the same?

I can create a generic class and use "where" statement like: public class TestClass where T: IDisposable { .... } To be sure that the type "T" is implement the interface 'IDisposable'. Is there some way to create generic class with 2 types…
user436862
  • 867
  • 2
  • 15
  • 32
3
votes
1 answer

scala type signature to allow numeric ops on members

I have two case classes that are basically identical, except that one has Int and the other Double members. I'm trying to create a common trait that I can use for functions operating on either and allowing basic numeric operations. But so far i…
Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
3
votes
1 answer

.NET Generics Type Constraint Inequality

I'm trying to implement two generic interfaces. It makes sense that ITwoWayMapper would implement both IOneWayMapper and IOneWayMapper. So if I try to do just that: public interface IOneWayMapper { U Map(T source); } public…
trousyt
  • 360
  • 2
  • 12
3
votes
5 answers

Cannot pass variable of type conforming to generic constraint

public interface ILovable where T : IEquatable { T Care(T t); } public class Me : ILovable { public int Care(int i) { return i; } } Say I have the above. Now below function fails: private static void…
nawfal
  • 70,104
  • 56
  • 326
  • 368
3
votes
1 answer

C# Generics and Generic Constraints

I'm writing a generic abstract class A with type parameter T that I intend to derive with class B. A has a data member, mX, an instance of class C, with a generic function. This generic function, GetAllOfType(), has one type parameter, T. This type…
2
votes
1 answer

Is it possible to exclude "this" from Type Inference in TypeScript?

I have the code below as a generic function to get all parents in a Tree type of structure. When I use it in a class though, I'm getting a Type Error. type GetParentFunc = (x:T)=>T; function getAllParents(obj:T,…
Dirk Boer
  • 8,522
  • 13
  • 63
  • 111
2
votes
1 answer

Swift Task extension: Success == Sendable, Failure == Error necessary?

Seeing in the Apple doc that the Task is defined as: @frozen struct Task where Success : Sendable, Failure : Error I've seen a number of code online where people add extensions to Task with constraint like: extension Task where…
Heuristic
  • 5,087
  • 9
  • 54
  • 94
2
votes
1 answer

Is there a way to detect the notnull constraint via reflection on a generic method?

When trying to get constraints applied to a generic method with reflection, after calling GetGenericArguments on the MethodInfo, if I use GenericParameterAttributes I get None and GetGenericParameterConstraints returns an empty Type[] when there's…
nalka
  • 1,894
  • 11
  • 26
2
votes
0 answers

C# generic constraints: Force two generic types to be different

I've got a class with two generics and two indexers (such that t1->t2 and t2->t1.) public class Mapper { private readonly Dictionary _forward; private readonly Dictionary _reverse; public T2 this[T1 val] { …
Jakob Lovern
  • 1,301
  • 7
  • 24
2
votes
0 answers

Why does tsc inline generic constraint?

I have a library where I added this function length: const length = < T extends AnyArray, S extends LengthComparison, R extends ToTuple, ExtractLength> >( array: T, condition: S ): array is R extends T ? R : never The…
Beraliv
  • 518
  • 7
  • 20
2
votes
1 answer

Enums in Generic constraints

I built a extension method for Enums (Enumerations) - , name it, say GetEnumSecondName static string GetEnumSecondName(this Enum myEnumInstance) {...} Now, I have a generic method, that should take a Enumeration and return all the second names…
serhio
  • 28,010
  • 62
  • 221
  • 374