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).
generics
49,952
questions
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';
}
...
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 ...
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 = {
...
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>.
...
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 ...
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<...
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() ...
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();
}
}
...
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 ...
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 {
...
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: ...
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()...
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 ...
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 ...
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&...