generics

Calling non generic function from generic context swift

杀马特。学长 韩版系。学妹 提交于 2021-01-28 21:39:46
问题 I'm having issues with understanding how matching types in generic functions work in swift. I do not understand why my code is not compiling. This is what I have: enum LoadingState<T> { case idle case loading case failed(Error) case loaded(T) } private func updateInternal(_ state: Int) { print("=int") } private func updateInternal(_ state: String) { print("=string") } private func update<T>(_ state: LoadingState<T>) { switch state { case .loaded(let viewModel): updateInternal(viewModel)

TypeScript generics: Inequivalence of generic function type and call signature of an object literal type (Type 'T' is not assignable to type 'T')

丶灬走出姿态 提交于 2021-01-28 16:31:39
问题 I'm writing an RxJS operator which waits for the input operator to complete and then does a switchMap to a second operator (which is generated on the fly just as with switchMap ). I have come up with two versions of my code, one which works and one which doesn't and I'm struggling to wrap my head around why that is. The version that works: import { Observable } from "rxjs"; // OperatorFunction, import { defaultIfEmpty, last, switchMap } from "rxjs/operators"; // This definition of

Java Generics List and ArrayList with and without Parameters

蓝咒 提交于 2021-01-28 13:10:35
问题 I'm reading about Java Generics. and I want to ask what is the difference between the following statements. 1: List<String> list = new ArrayList(3); 2: List<String> list = new ArrayList<String>(2); 3: List<String> list = new ArrayList<String>(); 4a: List<String> list = new ArrayList("A"); // why I can't use String? 4b: List<String> list = new ArrayList('a'); // but char works fine. I'm reading Java Docs on Generics and after that I need to ask the above questions because I didn't get exact

Java Generics List and ArrayList with and without Parameters

纵然是瞬间 提交于 2021-01-28 13:08:46
问题 I'm reading about Java Generics. and I want to ask what is the difference between the following statements. 1: List<String> list = new ArrayList(3); 2: List<String> list = new ArrayList<String>(2); 3: List<String> list = new ArrayList<String>(); 4a: List<String> list = new ArrayList("A"); // why I can't use String? 4b: List<String> list = new ArrayList('a'); // but char works fine. I'm reading Java Docs on Generics and after that I need to ask the above questions because I didn't get exact

Are there any Pitfalls to this approach which I am not seeing

我与影子孤独终老i 提交于 2021-01-28 12:59:12
问题 Service Registration services.AddScoped(typeof(PageStorageService<>), typeof(PageStorageService<>)); Service Declaration public interface IStoredPage<T> { T PageState { get; set; } } public class PageStorageService<T> : IStoredPage<T> { public T PageState { get; set; } } Usage in CounterPage @page "/counter" @using BlazorApp1.Data <h1>Counter</h1> <p>Current count: @currentCount</p> @inject PageStorageService<Counter> StorageService <button class="btn btn-primary" @onclick="IncrementCount"

What does <T> before the return type of a method mean?

南笙酒味 提交于 2021-01-28 11:15:17
问题 The following method returns a List composed of T type elements: public <T> List<T> getList() { return new ArrayList<T>(); } In the signature we have <T> List<T> . The List<T> makes sense, because that is the type of the return value . What is the need for the preceding <T> though? The code doesn't compile without both <T> and List<T> . Leaving out the <T> gives Cannot resolve symbol T I have read the official Oracle tutorial on generic methods. It explains that this is part of the syntax:

Java generics: Map nested json response to Java objects

我是研究僧i 提交于 2021-01-28 11:14:34
问题 Scenario: I'm working with an unusual external API in which every attribute is a map with multiple values. In order to convert this response into simple Java objects, I had to do some dirty unboxing. Below is one of the typical java class. As you can see how I'm unboxing data from response and mapping them to my java class: import com.fasterxml.jackson.annotation.JsonProperty; import lombok.*; import java.time.LocalDate; import java.util.Map; import static com.my.util.BaseUtil.unbox; @Getter

How correctly use Gson to deserialize an object of generic type through a static utility method?

与世无争的帅哥 提交于 2021-01-28 08:57:29
问题 I've seen a number of other similar looking questions, but I think there's a level of abstraction on top of those that makes the difference. Namely, I have a utility class with a static generic wrapper method to deserialize an object of generic type (unknown at build time): public final class Utils { public static final Gson sGson = new Gson(); public static synchronized <T> T fromJson(String doc) { return sGson.fromJson(doc, new TypeToken<T>(){}.getType()); } } A simple class to test it on:

TypeScript generic base on property value

随声附和 提交于 2021-01-28 08:07:20
问题 I want to have an interface where the generic type is based on the value of a property in this interface: interface Book<T extends BookType> { type: T, location: Shelf<T>, } Instead of using the following to define the generic type: const book: Book<BookType.Technology>; I want const book: Book<???>; book.type = BookType.Technology; And afterwards, location is "hinted" to have type Shelf<BookType.Technology> Is this possible in TypeScript? 回答1: TypeScript doesn't really support making empty

swift generic and dynamic method dispatch

≡放荡痞女 提交于 2021-01-28 06:57:33
问题 I create an extension for UITableViewCell, and give the default method. Through the set<T: UITableViewCell> method, I want the setupData method can dynamic dispatch by the cell type. But it always failed, and the result jump to the fatalError. import UIKit class cell: UITableViewCell { func setupData<T>(_ data: T) { print(#function) } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() set(cell.self) } func set<T: UITableViewCell>(_ t: T.Type) { let