Using a generic model in ASP.NET MVC Razor
given that @model is expecting a type – not a type declaration you could use: @model DtoViewModel<IDto> and take advantage of generic covariance
given that @model is expecting a type – not a type declaration you could use: @model DtoViewModel<IDto> and take advantage of generic covariance
This can be achieved using protocol extensions (See The Swift Programming Language: Protocols for more information). To sum just Ints you could do: extension Sequence where Iterator.Element == Int { var sum: Int { return reduce(0, +) } } Usage: let nums = [1, 2, 3, 4] print(nums.sum) // Prints: “10” Or, for something more … Read more
There are multiple reasons : Widgets are immutable. Since StatefulWidget extends Widget it therefore must be immutable too. Splitting the declaration into two classes allows both StatefulWidget to be immutable and State to be mutable. Widgets are instantiated using the syntax new MyWidget(). If we merged both classes into one, new MyWidget() would reset all … Read more
The generic type of the Promise should correspond to the non-error return-type of the function. The error is implicitly of type any and is not specified in the Promise generic type. So for example: function test(arg: string): Promise<number> { return new Promise<number>((resolve, reject) => { if (arg === “a”) { resolve(1); } else { reject(“1”); … Read more
Yes, you can. Try this: interface IBaz { baz: number; [key: string]: any; } function foo(bar: IBaz) : number { return bar.baz; } foo({ baz: 1, other: 2 });
It means that the trait is optional. The current syntax was introduced in the DST syntax RFC. The only trait I am aware of that works for ? is Sized. In this specific example, we can implement BorrowMut for unsized types, like [T] — note that there’s no & here! One built-in implementation makes use … Read more
Use Compound Type: trait Narrowable[A] extends Iterable[A] { def narrow[B <: A with AnyRef] : Iterable[B] }
Dictionary<string, string> is a more modern approach. It implements IEnumerable<T> and it’s more suited for LINQy stuff. StringDictionary is the old school way. It was there before generics days. I would use it only when interfacing with legacy code.
Just use the non-generic registration overloads (the ones where you need to pass the 2 Type objects.) Then provide the open generic types of both your interface and the implementation: services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>)); In your controller, add a dependency for a repository of a specific type (a closed generic type): public HomeController(IGenericRepository<Lookup1> repository) { … }
I have to say @Alex want to check if T type conforms to protocol rather than s. And some answerer didn’t see clearly. Check T type conforms to protocol like this : if let _ = T.self as? MyProtocol.Type { // T conform MyProtocol } or if T.self is MyProtocol.Type { // T conform MyProtocol … Read more