generics

Conversion from string to generic type

只谈情不闲聊 提交于 2020-03-18 05:46:08
问题 I need to convert string to variable value. I found solution only for C#. I need it in Java. public class Property<T> { T value; public void setValue(String input){ if(value instanceof String){ value= input; // value is type of T not type of string (compilation error) // incompatible types: String cannot be converted to T } if(value instanceof int){ //parse string } if(value instanceof boolean){ //parse string } ... } } 回答1: That is not how it works. You can, however, use polymorphism, to

Dynamic generic type inference for object literals in TypeScript

99封情书 提交于 2020-03-18 05:07:06
问题 In typescript, I can declare a generic function like so: const fn: <T>(arg: T)=>Partial<T> In this case, TypeScript can sometimes infer the type parameter of the function based on the actual parameters I pass it. Is there a similar way to define a generic object literal whose type parameter can be dynamically inferred based on its contents? Something like: interface XYZ { obj: <T>{ arr: T[], dict: Partial<T> } } I am aware I can make the entire interface generic like so: interface XYZ<T> {

Dynamic generic type inference for object literals in TypeScript

不想你离开。 提交于 2020-03-18 05:06:47
问题 In typescript, I can declare a generic function like so: const fn: <T>(arg: T)=>Partial<T> In this case, TypeScript can sometimes infer the type parameter of the function based on the actual parameters I pass it. Is there a similar way to define a generic object literal whose type parameter can be dynamically inferred based on its contents? Something like: interface XYZ { obj: <T>{ arr: T[], dict: Partial<T> } } I am aware I can make the entire interface generic like so: interface XYZ<T> {

List<? extends Base> VS List<Base>

别说谁变了你拦得住时间么 提交于 2020-03-18 03:40:40
问题 List<? extends Base> list List<Base> list Is there any difference between the two declarations? Thanks, 回答1: Yes. List<Base> can contain a mixture of different things that all derive from Base . List<? extend Base> contains homogeneous items (in the sense that they must all derive from some specific, unknown type that in turn derives from Base ). Put another way, List<? extends Base> is the base class for List<T extends Base> . So you can pass a List<T extends Base> to any method that takes a

how does scala's type check work in this case?

最后都变了- 提交于 2020-03-17 09:19:50
问题 // Start writing your ScalaFiddle code here sealed trait DSL[A]{ // def run(): A ={ // this match { // case GetLength(something) => // something.length // case ShowResult(number) => // s"the length is $number" // } // } } case class GetLength(something: String) extends DSL[Int] case class ShowResult(number: Int) extends DSL[String] def run[A](fa:DSL[A]): A ={ fa match { case GetLength(something) => something.length case ShowResult(number) => s"the length is $number" } } val dslGetLength =

What is the difference between <? extends Base> and <T extends Base>?

我是研究僧i 提交于 2020-03-17 05:18:07
问题 In this example: import java.util.*; public class Example { static void doesntCompile(Map<Integer, List<? extends Number>> map) {} static <T extends Number> void compiles(Map<Integer, List<T>> map) {} static void function(List<? extends Number> outer) { doesntCompile(new HashMap<Integer, List<Integer>>()); compiles(new HashMap<Integer, List<Integer>>()); } } doesntCompile() fails to compile with: Example.java:9: error: incompatible types: HashMap<Integer,List<Integer>> cannot be converted to

Generic class to return particular set of data depending on type

依然范特西╮ 提交于 2020-03-16 09:22:30
问题 I have an interface public interface IFetchData<TEntity> { IEnumerable<TEntity> GetItems(); } Two classes inherit from this interface FetchFromDatabase and FetchFromCollection . The purpose is to switch between classes injected to another class that let's say present them on screen etc. Depending on type used, I would like to fetch data from a particular collection depending on a type. It was not a problem to implement this functionality in FetchFromDatabase because DbContext has method

How to look into generic tList during Delphi debugging

北城余情 提交于 2020-03-16 05:54:06
问题 I use Delphi 10.3.1 COMMUNITY version and can't look into generic tList while I debug the project. I know the latest version of Delphi doesn't support the old-typed debug feature which allows to look into generic tList. So I used tList.List in the following code to evaluate the tList. In tList<tRecord>.List I can look into it but can't do it in tList<Integer>.List . type tRecord = record Field: Integer; end; procedure TForm1.FormCreate(Sender: TObject); var _Record: tRecord; _List1: TList

A function with generic return type

不打扰是莪最后的温柔 提交于 2020-03-16 05:22:27
问题 Is it possible to have a function that returns a generic type? Something like: fun <T> doSomething() : T { when { T is Boolean -> somethingThatReturnsBoolean() T is Int -> somethingThatReturnsInt() else -> throw Exception("Unhandled return type") } } 回答1: You can use reified type to capture its class literal, like this: inline fun <reified T> doSomething() : T { return when (T::class.java) { Boolean::class.java -> somethingThatReturnsBoolean() as T Int::class.java -> somethingThatReturnsInt()

A function with generic return type

我与影子孤独终老i 提交于 2020-03-16 05:20:22
问题 Is it possible to have a function that returns a generic type? Something like: fun <T> doSomething() : T { when { T is Boolean -> somethingThatReturnsBoolean() T is Int -> somethingThatReturnsInt() else -> throw Exception("Unhandled return type") } } 回答1: You can use reified type to capture its class literal, like this: inline fun <reified T> doSomething() : T { return when (T::class.java) { Boolean::class.java -> somethingThatReturnsBoolean() as T Int::class.java -> somethingThatReturnsInt()