generics Questions

7

Solved

This is likely a a novice question, but google surprisingly did not provide an answer. I have this rather artificial method T HowToCast<T>(T t) { if (typeof(T) == typeof(string)) { T new...
Zootoxin asked 3/11, 2010 at 22:55

3

I have a struct NotificationOption and another struct NotificationOption2 as well as an implementation for From<NotificationOption> for NotificationOption2. I'm would like to convert Vec<N...
Phytobiology asked 16/12, 2020 at 18:23

4

Solved

I'm trying to understand the purpose of the reified keyword. Apparently, it's allowing us to do reflection on generics. However, when I leave it out, it works just as fine. When does this make an a...
Narrows asked 29/8, 2017 at 23:11

5

Solved

Is it possible to give an optional generic parameter a default value? I'm trying to do something like this: func addChannel<T>(name: String, data: T? = nil) -> Channel { } let myChanne...
Blazon asked 11/2, 2015 at 15:31

3

I have a Python package that defines a variety of collections based on the ABCs provided by collections.abc (Mapping, Sequence, etc). I want to take advantage of the type hinting facilities introdu...
Malcolm asked 14/10, 2015 at 7:33

18

Solved

.NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization. Arrays are very easy...
Tessie asked 21/1, 2009 at 20:54

6

Solved

Is it to maintain backwards compatibility with older (un-genericized) versions of Collection? Or is there a more subtle detail that I am missing? I see this pattern repeated in remove also (remove(...
Fleurette asked 8/6, 2010 at 0:43

4

Solved

I have a function which takes one argument of a generic type and I want to access the class of it: fun <T> test(t: T) { t::class } This fails with "expression in class literal has nullabl...
Brierroot asked 12/9, 2017 at 16:5

11

I am developing a mobile project made with Flutter. This project need to connect to some servers for REST consumption services (GET, POST, PUT, DELETE, ...), and retrieve data as well as send data ...
Tipster asked 22/3, 2019 at 19:39

1

Solved

I'm fairly new to Typescript and generics; I must be missing something hopefully trivial. I'm trying to pass a (generic) class as an argument for a function, but this class extends from another spe...
Bashaw asked 19/3, 2022 at 4:43

9

Solved

I was wondering what is the difference between the following two method declarations: public Object doSomething(Object obj) {....} public <T> T doSomething(T t) {....} Is there something ...
Maulmain asked 5/3, 2011 at 22:8

2

Solved

I am using ViewBinding and I am trying to reduce the code creating a Fragment that is an abstract class and contains this code: abstract class MyFragment<T> : Fragment() { private var bind...
Zonda asked 18/10, 2020 at 8:0

17

Solved

I came across PECS (short for Producer extends and Consumer super) while reading up on generics. Can someone explain to me how to use PECS to resolve confusion between extends and super?
Encrinite asked 27/4, 2010 at 17:16

3

Solved

We are trying to create a generic Category class. At the time being, we are unsure whether category will have integer or UUID as key. Hence, we need the id to be generic for now. All works fine. Ho...
Surly asked 4/3, 2022 at 6:45

3

Let's say I have this: val myAnon:(Option[String],String)=>String = (a:Option[String],defVal:String) => { a.getOrElse(defVal) } Don't mind what the function does. Is there anyway of makin...
Maneuver asked 31/3, 2010 at 16:9

5

I'm calling an api to get the an input stream and then call static method parseFrom(inputstream) to convert it to the protobuffclass. If I do it with a specific class it works: public CustomerDTO...
Enchorial asked 24/12, 2014 at 20:46

6

Solved

I have the following method: namespace ListHelper { public class ListHelper<T> { public static bool ContainsAllItems(List<T> a, List<T> b) { return b.TrueForAll(delegate(T t...
Carpo asked 5/10, 2009 at 15:2

4

Solved

I am trying to create a generic component where a user can pass the a custom OptionType to the component to get type checking all the way through. This component also required a React.forwardRef. ...
Polypeptide asked 20/10, 2019 at 0:58

7

Solved

I gather that you cannot bind a Java generics type parameter to a lower bound (i.e. using the super keyword). I was reading what the Angelika Langer Generics FAQ had to say on the subject. They say...
Zsolway asked 4/2, 2011 at 20:44

6

Solved

Consider this code: public class DummyClass { public List<? extends Number> dummyMethod() { return new ArrayList<Integer>(); } } public class DummyClassTest { public void testMoc...
Triboelectricity asked 9/9, 2011 at 18:59

2

Solved

Is it possible to mock (with mockito) method with signature Set<? extends Car> getCars() without supress warnings? i tried: XXX cars = xxx; when(owner.getCars()).thenReturn(cars); but no m...
Trass asked 11/5, 2012 at 16:9

2

I am using PostgreSQL and Hibernate 6, and I have a JSONB type column - values. The question is, how can I define an entity with generic type for this field? Also, is there any way to map the corre...
Validate asked 3/10, 2023 at 22:24

2

Solved

I am trying to define an interface with a few methods, and I would like one of the methods to be generic. It is a filterUnique method, so it should be able to filter lists of numbers, strings, etc...
Lyrist asked 21/9, 2015 at 21:12

4

In Swift I'm used to setting up a protocol JSONInitializable which defines an init?(json: JSON) meaning that all classes and structs that conform to the protocol are initializable with a JSON objec...
Barehanded asked 10/12, 2018 at 16:25

7

Solved

I'm trying to write code that validates type hints, and in order to do so I have to find out what kind of object the annotation is. For example, consider this snippet that's supposed to tell the us...
Speakeasy asked 8/3, 2018 at 10:45

© 2022 - 2025 — McMap. All rights reserved.