generics

C# Generic Operators

拈花ヽ惹草 提交于 2020-01-05 06:43:23
问题 I am trying to implement a generic operator like so: class Foo { public static T operator +<T>(T a, T b) { // Do something with a and b that makes sense for operator + here } } Really what I'm trying to do is gracefully handle inheritance. With a standard operator + in Foo, where T is instead "Foo", if anyone is derived from Foo (say Bar inherits Foo), then a Bar + Bar operation will still return a Foo. I was hoping to solve this with a generic operator +, but I just get a syntax error for

“Attempting to use incompatible return type” with class implementing Serializable

坚强是说给别人听的谎言 提交于 2020-01-05 06:14:48
问题 I have the following interface: Slicer public interface Slicer { Optional<Map<String, ? extends Serializable>> pointer(); } of which I have an implementation: DynamoDbSlicer public abstract class DynamoDbSlicer implements Slicer { @Override public abstract Optional<Map<String, AttributeValue> pointer(); } where AttributeValue is from the AWS SDK and defined as: public final class AttributeValue implements SdkPojo, Serializable, ToCopyableBuilder<AttributeValue.Builder, AttributeValue> { Note

“Attempting to use incompatible return type” with class implementing Serializable

混江龙づ霸主 提交于 2020-01-05 06:14:25
问题 I have the following interface: Slicer public interface Slicer { Optional<Map<String, ? extends Serializable>> pointer(); } of which I have an implementation: DynamoDbSlicer public abstract class DynamoDbSlicer implements Slicer { @Override public abstract Optional<Map<String, AttributeValue> pointer(); } where AttributeValue is from the AWS SDK and defined as: public final class AttributeValue implements SdkPojo, Serializable, ToCopyableBuilder<AttributeValue.Builder, AttributeValue> { Note

Using Mockito & Guice to test interfaces with generics in Scala

我是研究僧i 提交于 2020-01-05 05:57:16
问题 I am new to Scala, and I'm running into this problem when I'm trying to unit test some of my interfaces. I have an InputService trait with method def poll(parameters: HashMap[String, String]): Option[T] where T is generic, so InputService has a type parameter [T]. In my module, I have val inputService: InputService[String] = mock(classOf[InputService[String]]) bind[InputService[String]].toInstance(inputService) and in my InputServiceTest, I have var inputService: InputService[String] = _

Declare generic method returning enumeration

余生颓废 提交于 2020-01-05 05:19:54
问题 I want to implement with Scala analog of the following java code: static <T extends Enum> T getEnumByPrefix(String prefix, Class<T> enumClass) { for (T enumValue : enumClass.getEnumConstants()) { if (enumValue.name().startsWith(prefix)) { return enumValue; } } throw new NoSuchElementException(); } But I can't figure how to declare method for scala.Enumeration. I tried def enumByPrefix[T <: Enumeration](prefix: String): T.Value = ... but that doesn't compile. I tried def enumByPrefix[T <:

Extending ArrayList with determined default member type

青春壹個敷衍的年華 提交于 2020-01-05 05:07:39
问题 i am new at java, so i don't know what this technique called, and i may be poor at explain things, but i hpe you understand. Assume i have this two class, Item and it might be extended to ExtendedItem public class Item { } public class ExtendedItem extends Item {} i want to create collection wrapper for it, so i create a class extended from ArrayList... (Scenario 1) public class DataSet extends ArrayList<Item> {} then initialize it DataSet dataset1 = new DataSet(); DataSet<ExtendedItem>

How can I make this generic TypeScript function work as expected?

不羁岁月 提交于 2020-01-05 04:29:09
问题 I'm attempting to define a function that works well with the type system in TypeScript, such that I can take a key of an object and, if that key's value needs some modification (converting a custom string type to a boolean here in my example), I can do that without casting the types. Here's a TypeScript playground link that has the same walk-through but makes it easier to see the compiler errors. Some helper types to get my example started: type TTrueOrFalse = 'true' | 'false' const toBool =

Entity Framework: Generic compare type without Id?

早过忘川 提交于 2020-01-05 04:27:07
问题 Is there anyway to do a comparison between objects for equality generically without objects having an ID? I am trying to do a typical generic update, for which I have seen many examples of online, but they all usually look something like this: public void Update(TClass entity) { TClass oldEntity = _context.Set<TClass>().Find(entity.Id); foreach (var prop in typeof(TClass).GetProperties()) { prop.SetValue(oldEntity, prop.GetValue(entity, null), null); } } or something similar.The problem with

getting error message from server during API call

瘦欲@ 提交于 2020-01-05 04:15:11
问题 I have an app where I used RxSwift for my networking by extending ObservableType this works well but the issue I am having now is when I make an API request and there is an error, I am unable to show the particular error message sent from the server. Now how can I get the particular error response sent from the server extension ObservableType { func convert<T: EVObject>(to observableType: T.Type) -> Observable<T> where E: DataRequest { return self.flatMap({(request) -> Observable<T> in let

Why do classes need to be “final” when adopting a protocol with a property with type “Self”?

北战南征 提交于 2020-01-05 04:04:05
问题 I can use generic protocols just fine, but am trying to grasp the reasoning behind some limitations of generic protocols. Example: internal protocol ArchEnemyable { var archEnemy: Self? { get } } internal final class Humanoid: ArchEnemyable { internal var archEnemy: Humanoid? // is the adoption to Self, works only when class is final internal init(otherOne pOtherOne: Humanoid?) { self.archEnemy = pOtherOne } } I do not find any example where it would not work, when it is not final. Do one of