generics

Is there any reason to use generics in Java?

醉酒当歌 提交于 2020-12-10 05:20:31
问题 Perhaps I'm missing the point but coming from a C# background, I can't see any reason to use Java's generics... In C# I've got a method which takes a string and deserializes it into an object... public static Deserialize<T>(string source) { // Use reflection to examine T and determine // which properties I should be reading from // the string. Create an instance of T, // populate the properties and return it } which I can call as follows: x.Deserialize<AnyClass>("String representation of

Java Generics: Special usage <T extends Object & Interface>

孤街浪徒 提交于 2020-12-09 06:33:08
问题 I often find code which uses generics in Java like this: public static <T extends Object & Interface> foo(T object) { ... } Since in Java every class inherites from object class I'm not sure if extends Object gives a special meaning or is used for special purposes. Can anyone tell me if there is a different background using this or this is implicit given when you take <T extends Interface> ? 回答1: <T extends Object & Interface> That Object is clearly redundant and typically equals to <T

What is the correct way to implement trait with generics in Scala?

天大地大妈咪最大 提交于 2020-12-09 02:23:41
问题 I have some simple traits (Entity in the example below) that are extended by case classes in my app. I would like to create an EntityMapper trait that provides an interface for handling the case classes that extend the Entity trait (Foo in the example below). I thought I should be able to do this fairly easily using generics and bounding but I've spent a couple of hours on it already and I haven't gotten it to work correctly. The code below is what I think I should be able to do but it fails

Java generic Interface performance

二次信任 提交于 2020-12-08 06:25:23
问题 Simple question, but tricky answer I guess. Does using Generic Interfaces hurts performance? Example: public interface Stuff<T> { void hello(T var); } vs public interface Stuff { void hello(Integer var); <---- Integer used just as an example } My first thought is that it doesn't. Generics are just part of the language and the compiler will optimize it as though there were no generics (at least in this particular case of generic interfaces). Is this correct? 回答1: There is potential for a minor

Kotlin Generics declaration-site variance <in T> construction

眉间皱痕 提交于 2020-12-05 12:33:10
问题 I was reading about reasons why kotlin does not have wildcards (https://kotlinlang.org/docs/reference/generics.html). It all came to the declaration-site variance. We have <in T> and <out T> constructions which should replace wildcards. I think I understood how <out T> works but I have troubles with <in T> . So in java we could write something like this: public List<? extends Number> list1; public List<? super String> list2; First case after initialization becomes read only list (though not

How to solve “The issue with T?”/nullable constraint on type parameter?

久未见 提交于 2020-12-05 11:10:50
问题 I'm designing an interface in C# 8.0 with nullable enabled, targeting .Net Standard 2.0 (using the Nullable package) and 2.1. I am now facing The issue with T? . In my example, I am building an interface for a cache which stores Stream s/byte data, identified by a string key, i.e. the file system could by a trivial implementation. Every entry is additionally identified by a version, which should be generic. This version could for example be another string key (like an etag), an int or a date

Give names to Key and Value in C# Dictionary to improve code readability

左心房为你撑大大i 提交于 2020-12-04 18:11:11
问题 In C# struct, we can know clearly the purpose of a variable by it's name. For example, public struct Book { public string title; public string author; } Then, i know b.title is a type of string and it's referring to title. However in C# dictionary, we can only specify the type Dictionary<string,string> d How can i make the code more readable such that the key of the dictionary is type of string and it is referring to title , and the value is type of string and it is referring to author ? That

Give names to Key and Value in C# Dictionary to improve code readability

前提是你 提交于 2020-12-04 18:10:17
问题 In C# struct, we can know clearly the purpose of a variable by it's name. For example, public struct Book { public string title; public string author; } Then, i know b.title is a type of string and it's referring to title. However in C# dictionary, we can only specify the type Dictionary<string,string> d How can i make the code more readable such that the key of the dictionary is type of string and it is referring to title , and the value is type of string and it is referring to author ? That

Give names to Key and Value in C# Dictionary to improve code readability

北城余情 提交于 2020-12-04 18:07:59
问题 In C# struct, we can know clearly the purpose of a variable by it's name. For example, public struct Book { public string title; public string author; } Then, i know b.title is a type of string and it's referring to title. However in C# dictionary, we can only specify the type Dictionary<string,string> d How can i make the code more readable such that the key of the dictionary is type of string and it is referring to title , and the value is type of string and it is referring to author ? That

Give names to Key and Value in C# Dictionary to improve code readability

拈花ヽ惹草 提交于 2020-12-04 18:07:50
问题 In C# struct, we can know clearly the purpose of a variable by it's name. For example, public struct Book { public string title; public string author; } Then, i know b.title is a type of string and it's referring to title. However in C# dictionary, we can only specify the type Dictionary<string,string> d How can i make the code more readable such that the key of the dictionary is type of string and it is referring to title , and the value is type of string and it is referring to author ? That