Skip to main content

Questions tagged [generics]

Generics are a form of parametric polymorphism found in a range of languages, including .NET languages, Java, Swift, Rust and Go (since 1.18).

1 vote
0 answers
8 views

TS doesn't match exact type to generic? [duplicate]

type Input = 'a' | 'b'; type ResultOf<T extends Input> = T extends 'a' ? 'A' : 'B'; function foo<T extends Input>(input: T): ResultOf<T> { return input === 'a' ? 'A' : 'B'; } ...
Viktor Molokostov's user avatar
1 vote
1 answer
34 views

How to generically type-annotate a fuction/method parameter to accept any real number type?

I would like to write a generic function that takes an instance of any type that implements all real number operations (such as int, float, fractions.Fraction, or third-party types). I found the ...
I Like Python's user avatar
0 votes
0 answers
13 views

Constraining a generic parameter to be a record with a single key in Typescript

I would like to constrain the a generic parameter T of a class MyClass<T> such that T is valid for any record which has only 1 key: type ValidShape = { key: any; }; type InvalidShape = { ...
sbgrl's user avatar
  • 1
1 vote
1 answer
53 views

How to add numbers of a generic type

I have a collection of objects that have a unique key. That key could be numbers or a string, it's generic and most of the class doesn't care because it's stored in a Dictionary<TKey, TItem>. ...
ygoe's user avatar
  • 19.8k
0 votes
2 answers
127 views

Can not create Function array in Java 8

I have a class called SFunction which extends from Function in JDK 8 @FunctionalInterface public interface SFunction<T, R> extends Function<T, R>, Serializable { } Now I have several ...
flyingfox's user avatar
  • 13.5k
0 votes
0 answers
21 views

check if given object implements certain generic interface; type argument(s) NOT known [duplicate]

I have some object. I want to check at runtime if it is an IReadOnlyCollection<T> of any T, i.e. I cannot specify that T in my check. So it should return true for e.g. IReadOnlyCollection<...
Kjara's user avatar
  • 2,792
0 votes
1 answer
48 views

Java Generic wildcard misunderstanding [duplicate]

Having this: public <T extends DataAgent> DataProcessor<? extends DataAgent> resolveProcessor( final String response, T dAgent) throws Exception { if (dAgent.getType() ...
piet's user avatar
  • 396
0 votes
1 answer
63 views

Is it possible to mask a reference with an interface without heap allocating?

Having the following two classes interface IMask { string GetName(); } readonly struct StructSource : IMask { string IMask.GetName() { throw new NotImplementedException(); } } ...
James Jonatah's user avatar
0 votes
0 answers
56 views

How to simulate non-type template parameters in C# for generic classes for compile time type safety?

Is there a way to treat integers eg. 3 and 5 as their own types with an additional property being the value they represent? Specifically I want Modulo<3> to be different to Modulo<5> to ...
Mukund Srinivasan's user avatar
0 votes
0 answers
14 views

How do you transform a typescript object to another type, without using type casting? [duplicate]

I have a mapped type that maps strings/numbers/booleans to "tagged" versions of those types: interface TaggedString { type: "string"; value: string; } interface TaggedNumber { ...
lentil-soup's user avatar
0 votes
0 answers
26 views

Type 'any Shape' cannot conform to 'Shape'

I am trying to create different shapes on a card based on the card type in Swiftui. import SwiftUI let shapes: [any Shape] = [Circle(), Rectangle(), Ellipse()] struct CardView: View { var body: ...
Madu's user avatar
  • 4,939
1 vote
1 answer
51 views

Creating generics with delegate protocol type

I am trying to refactor this code: @objc protocol BaseDelegate { func xxx() } protocol DerivedDelegate: BaseDelegate { func yyy() } protocol NextDerivedDelegate: BaseDelegate { func zzz()...
Bawenang Rukmoko Pardian Putra's user avatar
0 votes
0 answers
43 views

SwiftUI store variety of generic functions for custom Table [closed]

Hello im still in my beginning of learning SwiftUI. Currently I try to build a expense tracker myself for some more advanced practice. For that reason I try to replicate a excel like pivot table for ...
Tom's user avatar
  • 1
0 votes
1 answer
12 views

Why is This _Generic Statement Giving an "Expression expected" Error?

I am writing an implementation of a dynamic array in C11. The keyword _Generic comes along with the C11 standard and is supported in my version of GCC (14.1.1) as far as I know. I have worked with it ...
Zenais's user avatar
  • 98
0 votes
1 answer
40 views

Infer the return type based on a parameter

I've a function that takes an action "DELETE"|"RENAME"|"MOVE"|etc... I would like the return type to be different depending on the action fired. For example, "DELETE&...
OtpExhaustv2's user avatar

15 30 50 per page
1
2 3 4 5
3331