generics

Use math operators on generic variables in a generic Java class

北城余情 提交于 2020-07-06 11:03:43
问题 I'm trying to write some code that will allow me to perform basic math operations on a "T extends Number" object instance. It needs to be able to handle any number type that is a subclass of Number . I know some of the types under Number have .add() methods built in, and some even have .multiply() methods. I need to be able to multiply two generic variables of any possible type. I've searched and searched and haven't been able to come up with a clear answer of any kind. public class Circle<T

Extra generic parameter in generic extension methods?

落爺英雄遲暮 提交于 2020-07-04 13:22:06
问题 I would like make an extension method for the generic class A which takes yet another generictype (in this example TC), but i guess that aint possible? class Program { static void Main(string[] args) { var a = new A<B, B>(); a.DoIt<B>(); } } static class Ext { public static A<TA, TB> DoIt<TA, TB, TC>(this A<TA, TB> a) { return a; } } class A<TA, TB> { } class B { } 回答1: If you can accept a slight syntax change, then it would be possible. Change it to: var a = new A<B, B>(); a.Do().It<B>();

Use Type Erasure return Generic Type in a function with Swift (Cannot convert return expression of type…)

五迷三道 提交于 2020-07-03 12:04:38
问题 I have a problem with generics in swift. Let's expose my code. protocol FooProtocol { associatedtype T } protocol Fooable { } extension Int : Fooable { } extension String: Fooable { } class AnyFoo<T>: FooProtocol { init<P: FooProtocol>(p: P) where P.T == T { } } class FooIntImpClass: FooProtocol { typealias T = Int } class FooStringImpClass: FooProtocol { typealias T = String } func createOne(isInt: Bool) -> AnyFoo<Fooable> { if isInt { let anyFoo = AnyFoo(p: FooIntImpClass()) return anyFoo }

Use Type Erasure return Generic Type in a function with Swift (Cannot convert return expression of type…)

為{幸葍}努か 提交于 2020-07-03 12:02:07
问题 I have a problem with generics in swift. Let's expose my code. protocol FooProtocol { associatedtype T } protocol Fooable { } extension Int : Fooable { } extension String: Fooable { } class AnyFoo<T>: FooProtocol { init<P: FooProtocol>(p: P) where P.T == T { } } class FooIntImpClass: FooProtocol { typealias T = Int } class FooStringImpClass: FooProtocol { typealias T = String } func createOne(isInt: Bool) -> AnyFoo<Fooable> { if isInt { let anyFoo = AnyFoo(p: FooIntImpClass()) return anyFoo }

C++ generic class error, what's the problem?

丶灬走出姿态 提交于 2020-06-29 06:43:11
问题 Why the following code doesn't compile? namespace mtm { template<class T> class Matrix { private: public: class AccessIllegalElement; }; Matrix::AccessIllegalElement{}; } I'm trying to implement the inner class for handling errors Error I get: 'Matrix' is not a class, namespace, or enumeration Plus, if inside AccessIllegalElement I want to write a function that prints the illegal index what is preferable? 1) to define a function that takes one parameter 2) to give every class object a member

kotlin geneirc with vararg parameter

半腔热情 提交于 2020-06-29 04:17:10
问题 Now I am working on some problems about passing the function as parameter in a function with vararg parameter with generic parameter. The below code works for one function as parameter into other function like that funA(funB(parameter):return1):return2 but when i make function like that funA(vararg funB(parameter):return1):return2 it doesn't works. I have tried somethings like Array or KFunction1 ViewModel fun callMultipleAPI( vararg observable: Observable<Any>):LiveData<Boolean>{ .....

Generic function that return different types based on an enum input

余生长醉 提交于 2020-06-28 03:43:32
问题 I have a struct which holds registers. I want my read_register function to return a u8 for Register::V0 and Register::V1 but a u16 for Register::V2 and Register::V3 . I'm not sure how to make the function generic for over the input type. I'm getting the error match arms have incompatible types which does make sense because the types are different. struct Registers { v0: u8, v1: u8, v2: u16, v3: u16, } enum Register { V0, V1, V2, V3, } impl Registers { fn read_register<T>(&self, register:

InvalidCastException while casting to OrganizationServiceContext

情到浓时终转凉″ 提交于 2020-06-28 03:42:27
问题 I have a early bound class generated by CrmSvcUtil: public partial class CustomerCrmServiceContext : Microsoft.Xrm.Sdk.Client.OrganizationServiceContext { ... ... } Then I have a class like this (short version): public abstract class PluginClass<T, C> : PluginClassBase<T> where T : Entity where C : OrganizationServiceContext { protected new C ServiceContext; protected PluginClass(IOrganizationService service, ITracingService tracer) : base(service, tracer) { ServiceContext = (C)base

Casting to a Tuple<object,object>

十年热恋 提交于 2020-06-27 19:22:01
问题 Consider this chunk of code where I'm testing an unknown variable that could be an int, MyObj, or Tuple, and I'm doing some type checking to see what it is so that I can go on and handle the data differently depending on what it is: class MyObj { } // ... void MyMethod(object data) { if (data is int) Console.Write("Datatype = int"); else if (data is MyObj) Console.Write("Datatype = MyObj"); else if (data is Tuple<object,object>) { var myTuple = data as Tuple<object,object>; if (myTuple.Item1

Casting to a Tuple<object,object>

眉间皱痕 提交于 2020-06-27 19:21:12
问题 Consider this chunk of code where I'm testing an unknown variable that could be an int, MyObj, or Tuple, and I'm doing some type checking to see what it is so that I can go on and handle the data differently depending on what it is: class MyObj { } // ... void MyMethod(object data) { if (data is int) Console.Write("Datatype = int"); else if (data is MyObj) Console.Write("Datatype = MyObj"); else if (data is Tuple<object,object>) { var myTuple = data as Tuple<object,object>; if (myTuple.Item1