generics

Java Generics <T> Meaning

天涯浪子 提交于 2021-02-16 09:35:06
问题 I was reading and reviewing the following site and had a question. https://www.geeksforgeeks.org/angle-bracket-in-java-with-examples/ In the explanations below they show class definitions followed by <T> and then when actually implementing these classes they use different types such as or as the parameters. My question is: is the '' notation actually a defined syntax in Java? In particular, is the T a necessary thing in order to define a "Generic"? And then does it basically mean that the

Java Generics <T> Meaning

谁说胖子不能爱 提交于 2021-02-16 09:35:02
问题 I was reading and reviewing the following site and had a question. https://www.geeksforgeeks.org/angle-bracket-in-java-with-examples/ In the explanations below they show class definitions followed by <T> and then when actually implementing these classes they use different types such as or as the parameters. My question is: is the '' notation actually a defined syntax in Java? In particular, is the T a necessary thing in order to define a "Generic"? And then does it basically mean that the

How exactly keyword 'params' work?

拜拜、爱过 提交于 2021-02-16 07:49:10
问题 The following code sample prints: T T[] T[] While first two lines are as expected, why compiler selected param array for a regular array? public class A { public void Print<T>(T t) { Console.WriteLine("T"); } public void Print<T>(params T[] t) { Console.WriteLine("T[]"); } } class Program { static void Main(string[] args) { A a = new A(); a.Print("string"); a.Print("string","string"); a.Print(new string[] {"a","b"}); } } 回答1: Under the hood a.Print("string","string"); is just syntactic sugar

Why does Optional.map make this assignment work?

时间秒杀一切 提交于 2021-02-15 12:07:29
问题 Optional<ArrayList<String>> option = Optional.of(new ArrayList<>()); Optional<ArrayList<?>> doesntWork = option; Optional<ArrayList<?>> works = option.map(list -> list); The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>> . Is there some sort of implicit cast going on? 回答1: If you look into the code of map

Why does Optional.map make this assignment work?

僤鯓⒐⒋嵵緔 提交于 2021-02-15 12:06:46
问题 Optional<ArrayList<String>> option = Optional.of(new ArrayList<>()); Optional<ArrayList<?>> doesntWork = option; Optional<ArrayList<?>> works = option.map(list -> list); The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>> . Is there some sort of implicit cast going on? 回答1: If you look into the code of map

Why does Optional.map make this assignment work?

亡梦爱人 提交于 2021-02-15 12:06:16
问题 Optional<ArrayList<String>> option = Optional.of(new ArrayList<>()); Optional<ArrayList<?>> doesntWork = option; Optional<ArrayList<?>> works = option.map(list -> list); The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>> . Is there some sort of implicit cast going on? 回答1: If you look into the code of map

Type of properties in a generic function

断了今生、忘了曾经 提交于 2021-02-15 06:54:57
问题 I'm trying to transform some fields of my object by providing field names, currently I wrote something like follows: interface Foo { a: number[], b: string[], } type Bar = { [T in keyof Foo] : (arg : Foo[T]) => Foo[T] } function test<T extends keyof Foo>(field: T) { const foo : Foo = { a: [], b: [], }; const bar: Bar = { a: arg => /* some code */ [], b: arg => /* some code */ [], }; foo[field] = bar[field](foo[field]); } But I end up with the following error message on bar[field](foo[field])

Why is generic specialization lost inside a generic function

自作多情 提交于 2021-02-15 04:53:25
问题 When I create a computed property that depends on a generic type, the specific implementation is "lost" when the instance is passed in a generic function. For example, I added the isBool on Array that returns true if Array.Element is Bool : extension Array { var isBool: Bool { false } } extension Array where Element == Bool { var isBool: Bool { true } } Using it directly on the instance works fine let boolArray: [Bool] = [true, false] let intArray: [Int] = [1, 0] boolArray.isBool // true

Why is generic specialization lost inside a generic function

孤街浪徒 提交于 2021-02-15 04:52:07
问题 When I create a computed property that depends on a generic type, the specific implementation is "lost" when the instance is passed in a generic function. For example, I added the isBool on Array that returns true if Array.Element is Bool : extension Array { var isBool: Bool { false } } extension Array where Element == Bool { var isBool: Bool { true } } Using it directly on the instance works fine let boolArray: [Bool] = [true, false] let intArray: [Int] = [1, 0] boolArray.isBool // true

Cast to generic where type is unknown

与世无争的帅哥 提交于 2021-02-13 17:32:19
问题 The following code is a simplified version what I have: public class Message { public int Prop1 { get; set; } public string Prop2 { get; set; } } public class ExtendedMessage<TExtension> : Message { public TExtension Extension { get; set; } } public class Processor<T> where T : Message { public void Process(T message) { } } I will have many types that inherit from either Message or ExtendedMessage. I would like to be able to use Processor to process those that inherit from ExtendedMessage<>