How to create a custom deserializer in Jackson for a generic type?

You can implement a custom JsonDeserializer for your generic type which also implements ContextualDeserializer. For example, suppose we have the following simple wrapper type that contains a generic value: public static class Wrapper<T> { public T value; } We now want to deserialize JSON that looks like this: { “name”: “Alice”, “age”: 37 } into … Read more

C# Generic Operators

No, you can’t declare generic operators in C#. Operators and inheritance don’t really mix well. If you want Foo + Foo to return a Foo and Bar + Bar to return a Bar, you will need to define one operator on each class. But, since operators are static, you won’t get the benefits of polymorphism … Read more

How do I return a Filter iterator from a function?

Rust 1.26 fn filter_one(input: &[u8]) -> impl Iterator<Item = &u8> { input.iter().filter(|&&x| x == 1) } fn main() { let nums = vec![1, 2, 3, 1, 2, 3]; let other: Vec<_> = filter_one(&nums).collect(); println!(“{:?}”, other); } Rust 1.0 fn filter_one<‘a>(input: &’a [u8]) -> Box<Iterator<Item = &’a u8> + ‘a> { Box::new(input.iter().filter(|&&x| x == 1)) } … Read more

Type erasure, overriding and generics

The signature of fooMethod(Class<?>) is the same as the signature of fooMethod(Class) after erasure, since the erasure of Class<?> is simply Class (JLS 4.6). Hence, fooMethod(Class) is a subsignature of the fooMethod(Class<?>) but not the opposite (JLS 8.4.2). For overriding with instance methods you need the overriding method to be a subsignature of the overridden … Read more

Pseudo-generics in C

You can do something like this in a header file: // // generic.h // #define TOKENPASTE(x, y) x ## y #define GET_MINIMUM(T) TOKENPASTE(get_minimum_, T) TYPE GET_MINIMUM (TYPE) (TYPE * nums, size_t len){ TYPE min = nums[0]; for (size_t i = 1; i < len; i++) { if (nums[i] < min) { min = nums[i]; } … Read more