generics

Redundancy in C#?

假如想象 提交于 2020-01-12 06:59:27
问题 Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by the language designers? If so, why? 回答1: The reason the code appears to be redundant is because, to a novice programmer, it appears to be defining the same thing twice. But this is not what the code is doing. It is defining two separate things that just happen to be of the same type. It is defining the following: A variable named distances of type List<int> . An object on the heap of type List

Defining implicit view-bounds on Scala traits

青春壹個敷衍的年華 提交于 2020-01-12 06:51:23
问题 I'm doing an exercise to implement a functional binary-search-tree in Scala, following a similar pattern that I've seen used in Haskell. I have a structure that looks something like this: trait TreeNode[A] { def isLeaf: Boolean def traverse: Seq[A] ... } case class Branch[A](value: A, left: TreeNode[A], right: TreeNode[A]) extends TreeNode[A] { def isLeaf: Boolean = false def traverse: Seq[A] = ... ... } case class Leaf[A]() extends TreeNode[A] { def isLeaf: Boolean = true def traverse: Seq[A

Type safety with generics in Java

拥有回忆 提交于 2020-01-12 05:02:46
问题 I've encountered a behaviour of generics in Java that I completely can't understand (with my .NET background). public class TestGeneric<T> { public void get (Object arg) { T temp = (T) arg; System.out.println(temp.toString()); return; } } TestGeneric<Integer> tg = new TestGeneric<Integer>(); tg.get("Crack!!!"); Please tell me why I'm not getting ClassCastException in get, moreover, in Idea I see temp as String after assignment and having value of "Crack!!!" . Also, how could I have that

Why does a generic cast of a List<? extends Set..> to List<Set..> succeed on Sun JDK 6 but fail to compile on Oracle JDK 7?

爷,独闯天下 提交于 2020-01-12 04:41:07
问题 The following code class GenericCompilationFailureDemo { List<? extends GenericCompilationFailureDemo> newList() { return new ArrayList<GenericCompilationFailureDemo>(); }; void useList() { List<GenericCompilationFailureDemo> list = (List<GenericCompilationFailureDemo>) newList(); } List<? extends Set<GenericCompilationFailureDemo>> newListOfSpecificSets() { return new ArrayList<Set<GenericCompilationFailureDemo>>(); }; void useListOfSpecificSets() { List<Set<GenericCompilationFailureDemo>>

Can all usages of `forSome` be replaced by an equivalent usage of `_`?

亡梦爱人 提交于 2020-01-12 04:35:11
问题 For instance, List[T] forSome { type T } is equivalent to List[_] , but is this true for every possible usage of forSome or are there cases where forSome cannot be replaced by an equivalent of the second syntax? 回答1: No, not all usages can be thus converted. Something like this (thanks to retronym , below, who should be getting the upvotes on this one) def foo(xs: Map[T, T] forSome { type T}) The point here is that I can use the existential in more than one place but it is the same

How to implement “equals” method for generics using “instanceof”?

人走茶凉 提交于 2020-01-11 16:36:04
问题 I have a class that accepts a generic type , and I want to override the equals method in a non-awkward way (i.e. something that looks clean and has minimal amount of code, but for a very general use case). Right now I have something like this: public class SingularNode<T> { private T value; @SuppressWarnings("unchecked") @Override public boolean equals(Object other){ if(other instanceof SingularNode<?>){ if(((SingularNode<T>)other).value.equals(value)){ return true; } } return false; } }

C# 8 gives a warning when returning a nullable generic with nullable constraint

一世执手 提交于 2020-01-11 14:41:07
问题 This code: public T Foo<T>() where T : class? { return null; } Gives a following error: A null literal introduces a null value when 'T' is a non-nullable reference type I don't see why we can't return null when we say that T can be nullable. If we additionally try to return T? we will get an error that T has to be non-nullable. It seems it's kind of impossible to have a nullable constraint and return a nullable result at the same time. 回答1: Imagine you call: string result = Foo<string>();

How do I cast from int to generic type Integer?

前提是你 提交于 2020-01-11 13:24:07
问题 I'm relatively new to Java and am used to generics in C# so have struggled a bit with this code. Basically I want a generic method for getting a stored Android preference by key and this code, albeit ugly, works for a Boolean but not an Integer, when it blows up with a ClassCastException. Can anyone tell me why this is wrong and maybe help me improve the whole routine (using wildcards?)? public static <T> T getPreference(Class<T> argType, String prefKey, T defaultValue, SharedPreferences

Why a List<type> absorbs not-type element in compilation and execution time?

爷,独闯天下 提交于 2020-01-11 11:35:48
问题 I have this demo, which I dont need a particular solution with a redrawn architecture, but just understanding why behaves like that and any thing I am missing for avoiding it. I am wondering why: Compiler allows the insertion of an element that its not the type of the list into the list The ClassCast exception is thrown when we try to get the element instead of when pushing it import Test.*; //Inner classes import java.util.List; import java.util.ArrayList; public class Test<E extends Object>

What is the syntax for nested generic types in Genie?

狂风中的少年 提交于 2020-01-11 11:34:12
问题 I want to declare a HasTable with string as it's key and array of int as it's value: [indent=4] init var h = new HashTable of string, array of int (str_hash, str_equal) h["a"] = {1, 2, 3} h["b"] = {5, 6, 7} Error message: nested_generic_types.gs:4.27-4.28: error: syntax error, expected line end or semicolon but got `of' var h = new HashTable of string, array of int (str_hash, str_equal) So the double of seems to confuse valac here. What is the proper syntax? 回答1: The error message is diffrent