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

F# type constraints for vector operations

I'd like to write some generic functions and types in F# to work with vectors. I have multiple different data types with static (+) and (*) operators so I can add them and multiply them by scalars (floats for now). For instance, I've successfully…
Paul Orland
  • 542
  • 4
  • 16
4
votes
2 answers

C# - Stack Push operation does not work even after Correct Generic constraint

namespace ConsoleApp3 { class Program { // Main method - entry point of program static void Main(string[] args) { var animals = new Stack(); ZooCleaner.Wash(animals); } } …
Mysterious Jack
  • 621
  • 6
  • 18
4
votes
3 answers

C# Naked type constraints

In "C# 6.0 in a Nutshell" there is an example of naked type constarint usage: class Stack { Stack FilteredStack() where U : T {...} } Honestly, I don't understand why I should use this constraint here. If I'll remove it and change U to…
Ed Akhmetshin
  • 173
  • 10
4
votes
1 answer

Cannot compile constrained generic method

Long story short: The following piece of code does not compile in Delphi 10.1 Berlin (Update 2). interface uses System.Classes, System.SysUtils; type TTest = class(TObject) public function BuildComponent(const…
4
votes
2 answers

Generic types references each other

I write simple parser and want to implement next two interfaces: public interface IResult where TToken : ITokenizer, TValue> { TToken Tokenizer { get; } TValue Value { get; } } public interface…
Nikita Sivukhin
  • 2,370
  • 3
  • 16
  • 33
4
votes
1 answer

Type extensions with a generic constraint not raising an error, nor doing what might be expected

Consider the following code snippet using F# 4.0, .NET 4.6: type X<'T> = Y of 'T type XS = X type XI = X type X<'T when 'T :> string> with static member X = 2 static member take (s: 'T) =…
Abel
  • 56,041
  • 24
  • 146
  • 247
4
votes
1 answer

Cannot assign protocol array to generic array

There are some codes below, some gives compile time error some doesn't. Is there a bug or do I miss something about generics here? 1) Doesn't work: class DataSource: NSObject { var dataObjects: [DataType] init
osrl
  • 8,168
  • 8
  • 36
  • 57
4
votes
1 answer

Generic type constraint for interface?

I can do this: void MyMethod() where T : class { } Is there anything like this? void MyMethod() where T : interface { } I'd prefer not to specify the interface name explicitly. The alternative is that I can pass in an argument that's…
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206
4
votes
2 answers

Constraining one generic with another in Swift

I've run into a problem where I have some protocol: protocol Baz { func bar(input:T) } The function bar is made generic because I don't want the protocol itself to have a Self(it needs to be useable in a collection). I have an implementation…
Abe Schneider
  • 977
  • 1
  • 11
  • 23
4
votes
2 answers

How to extract generic method constraints via reflection in C#?

Given an object of type System.Reflection.MethodInfo how can I extract generic parameter constraints? Somehow I can not find reasonable information about this.
Regis May
  • 3,070
  • 2
  • 30
  • 51
4
votes
2 answers

Constructing/making a generic type and turning a type constraint into a struct-as-base-type constraint

Normally we cannot constrain a type parameter T to deriving from a sealed type (such as a struct type). This would be meaningless because there is only one type which could fit, and as such there is no need for generics. So constraints like: where T…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
4
votes
2 answers

Is it possible to define a "not Nullable" constraint in a C# generic method?

In C#, the Nullable type does not satisfy the where struct generic constraint (while AFAK this is techically a struct). This can be used to specify that the generic argument has to be a non-nullable value type : T DoSomething() where T :…
tigrou
  • 4,236
  • 5
  • 33
  • 59
4
votes
1 answer

C# casting generic parameter to interface

I need help with casting generic paremetrs down to an interface. I have prebaked code like this: public interface InterFoo {...} public InterFoo specialFoo() where T : InterFoo {...} public InterFoo regularFoo() {...} and i want…
bentheiii
  • 451
  • 3
  • 15
3
votes
1 answer

F#: Member constraints to help create seemingly dynamic types

I've been looking into a way to add some duck typing to an F# method. SomeMethod(model:'a) = let someField = model.Test("") Where the parameter coming in has the Test method on it. I've seen notation like this: member inline public x.Testing<…
Programmin Tool
  • 6,507
  • 11
  • 50
  • 68
3
votes
3 answers

Generic constraint for bitwise operators

So I have a class which looks something like this: public class Foo { // ... } I have a method which uses the generic argument TKey as follows: public int Test(TKey val) { return val | 5; } I need to set constraints which ensure that…
Twenty
  • 5,234
  • 4
  • 32
  • 67
1 2
3
9 10