generics

Python typing: return type with generics like Clazz[T] as in Java Clazz<T>

我只是一个虾纸丫 提交于 2020-06-27 16:53:32
问题 So I am aware of pythons typing.Optional. But I wrote my own crude PyOptional (https://github.com/felixhertrampf/PyOptional/blob/master/PyOptional.py) and would like to combine Optional[T] with my PyOptional to PyOptional[T]. I am currently using Python 3.7 and tried extending typing.Optional. Some of my PyOptional class PyOptional: T: TypeVar = TypeVar("T") def __init__(self, obj: T): self.value: Any = obj def get(self) -> Optional[T]: return self.value def or_else(self, default) -> T:

Using polymorphic function as a parameter

天涯浪子 提交于 2020-06-27 16:40:22
问题 I have a function exec which accepts 3 parameters and applies a function f passed as first argument to the other two - p1 and p2 . def exec[T](f: (T, T) => Boolean, p1: T, p2: T) = f(p1, p2) Everything works fine if I declare in advance a function which will be passed as an argument. Somehow compiler can infer types for arguments of eq or in other words it can figure out that whatever in this case is Int // declaring a function with type parameter (polymorphic method) def eq[whatever](p1:

A bad casting to Generics parameter type does not throw ClassCastException in Java

不想你离开。 提交于 2020-06-27 08:37:09
问题 So, I have rather esoteric question. I'm trying to create a somewhat generic, but typed property collection system. It's reliant on a core assumption that seems to be erroneous. The code illustrates the issue: import java.lang.Integer; public class Test { private static Object mObj = new String("This should print"); public static void main(String[] args ) { String s = Test.<String>get(); System.out.println(s); try { // actual ClassCastException reported HERE int i = Test.<Integer>get(); }

Swift: generic overloads, definition of “more specialized”

只谈情不闲聊 提交于 2020-06-26 14:16:32
问题 In the below example, why is the foo(f) call ambiguous? I understand that the second overload could also apply with P == () , but why isn't the first one considered more specialized, and therefore a better match? func foo<R>(_ f: () -> R) { print("r") } func foo<P, R>(_ f: (P) -> R) { print("pr") } let f: () -> Int = { 42 } foo(f) // "Ambiguous use of 'foo'" 回答1: I'd say your problem is that you don't explicitely tell the compiler that P == () try the following code in a playground : Void

Understanding Mixed Context Bounds of Seq[AnyVal] and Seq[String]

拟墨画扇 提交于 2020-06-26 14:12:21
问题 Suppose I have some function that should take a sequence of Ints or a sequence of Strings. My attempt: object Example extends App { import scala.util.Random val rand: Random.type = scala.util.Random // raw data val x = Seq(1, 2, 3, 4, 5).map(e => e + rand.nextDouble()) val y = Seq("chc", "asas") def f1[T <: AnyVal](seq: Seq[T]) = { println(seq(0)) } // this works fine as expected f1(x) // how can i combine f1(y) } How can I add this to also work with strings? If I change the method signature

How to Convert Seq to Json using Circe inside a function - keep getting “implicit value not found” error

前提是你 提交于 2020-06-26 12:11:05
问题 I am learning Circe and Scala for a project at work. To explain my issue, start with the following example: import io.circe.syntax._ object TestDrive extends App { val labels = Seq("Banana", "Banano", "Grapefruit") println(labels.asJson) } Ok so this outputs: ["Banana","Banano","Grapefruit"] This is good. Now I want to make my code a bit more general. I want to write a function that takes in a Sequence, whose elements can be of type AnyVal. Here is my attempt: import io.circe.syntax._ import

How to Convert Seq to Json using Circe inside a function - keep getting “implicit value not found” error

二次信任 提交于 2020-06-26 12:10:30
问题 I am learning Circe and Scala for a project at work. To explain my issue, start with the following example: import io.circe.syntax._ object TestDrive extends App { val labels = Seq("Banana", "Banano", "Grapefruit") println(labels.asJson) } Ok so this outputs: ["Banana","Banano","Grapefruit"] This is good. Now I want to make my code a bit more general. I want to write a function that takes in a Sequence, whose elements can be of type AnyVal. Here is my attempt: import io.circe.syntax._ import

Swift: Specialize method of generic class for function types

≯℡__Kan透↙ 提交于 2020-06-25 18:18:34
问题 For generic free functions I can use overloading, to essentially specialize the function for function types, like this: func foo<T>(_ t: T.Type) { print("T is unknown") } func foo<P>(_ t: ((P) -> Void).Type) { print("T is a function with one parameter") } let f: (String) -> Void = { print($0) } foo(type(of: f)) // prints "T is a function with one parameter" Note the second version of foo() is not protocol-constrained, mainly because as far as I know, we can't make function types conform to

Java and generics. Isn't 0 a Number?

只谈情不闲聊 提交于 2020-06-25 09:13:29
问题 What is that I'm missing about this snippet of code? public class Zero<N extends Number> { public N zero() { return new Integer(0); } } It says: Type mismatch: cannot convert from Integer to N Thanks! Update I've changed the snippet to use an integer. Same thing happens. And it happens even when creating an anonymous subclass of Number . Could it be Eclipse that is faulty about this? 回答1: While an Integer is a Number, an Integer might not be compatible to N which can be any subclass of Number

Java generics incompatible types (no instance(s) of type variable(s) T exist)

柔情痞子 提交于 2020-06-25 07:50:20
问题 That's basically my first touch with Java generic types and I can't figure out what is wrong with the following piece of code. I have a helper class Helper with a static function inRange usng generic type that should return the list of objects from an input list that are in certain range around object at index index (I haven't tested it yet, it's not an issue here if it works correctly or not): public class Helper { public static <T> List<T> inRange(List<T> list, int index, int range) { List