Questions tagged [generic-method]

180 questions
8
votes
4 answers

Ambiguous match found when using reflection to find Generic method

I'm using reflection to find the generic method for Newtonsoft JsonConvert.DeserializedObject but am finding that it's returning an ambiguous match for the non-generic version JsonConvert.DeserializeObject, here is the code which tries to get the…
DGibbs
  • 14,316
  • 7
  • 44
  • 83
8
votes
4 answers

Why type parameter required before return type for static generic methods

The following noGood method gives a compilation error because it omits the formal type parameter immediately before the return type T. public static T noGood(T t) { return t; } Could somebody please help me understand that why is it required for…
skip
  • 12,193
  • 32
  • 113
  • 153
8
votes
2 answers

Java: Defining a generic method inside an anonymous class

The following java code works fine. public static void main(String[] arg){ JPanel p = (new JPanel()); p.add( new Object(){ JButton f(JButton x){ x.setEnabled(false); return x; }}.f(new JButton("B"))…
user7841452
  • 101
  • 6
8
votes
4 answers

C#: How to use generic method with "out" variable

I want to create a simple generic function void Assign(out T result) { Type type = typeof(T); if (type.Name == "String") { // result = "hello"; } else if (type.Name == "Int32") { // result = 100; } else result =…
Tin Vo
  • 81
  • 1
  • 3
8
votes
2 answers

How to get MethodInfo of a generic method on a non generic .NET type?

I have this little problem, that I cannot figure out which arguments to pass to Type.GetMethod in order to get back the MethodInfo of a generic method on a non generic type. Specifically, I have this type definition: public static class A { public…
mark
  • 59,016
  • 79
  • 296
  • 580
7
votes
1 answer

How can a parameter in a Generic method be assigned to an Integer and a Character class at the same time?

Why this code isn't showing any compilation error? public class Generic { public static void main(String[] args) { Character[] arr3={'a','b','c','d','e','f','g'}; Integer a=97; …
Frosted Cupcake
  • 1,909
  • 2
  • 20
  • 42
6
votes
2 answers

Java method with generic return Type

Is there a way in Java to return different types with one declaration of a method? public Object loadSerialized(String path) { Object tmpObject; try { FileInputStream fis = new FileInputStream(path); ObjectInputStream ois =…
Hard_Veur
  • 401
  • 1
  • 4
  • 12
6
votes
3 answers

How generic methods gets instantiated in C#?

Suppose I've a generic method as: void Fun(FunArg arg) {} Are this.Fun and this.Fun different instantiations of the generic method? In general, how does the generic method get instantiated? Different generic argument produces…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
5
votes
4 answers

How to avoid "Type mismatch" in static generic factory method?

Either I'm too stupid to use google, or nobody else encountered this problem so far. I'm trying to compile the following code: public interface MyClass { public class Util { private static MyClass _this; public static T…
MarioP
  • 3,752
  • 1
  • 23
  • 32
5
votes
2 answers

How can a concise closure be written for a generic method?

I want to write an implementation of a functional, non-generic interface which has a generic method. The implementation needs to be an inline closure and concise. As a simplified example @FunctionalInterface interface Fn { R fn(R…
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
5
votes
1 answer

Why method defined like "cons[B >: A](v: B)" accepts argument of type which is not supertype of A?

I am studying variance in scala right now, and I think I have a good understanding of contravariance. For example given trait List[-A], I know that List[Int] is a supertype of List[AnyVal]. But say that I have the following trait: trait List[+A] { …
testing
  • 2,303
  • 4
  • 20
  • 32
5
votes
1 answer

Different behaviour for generic method return value to method and to assignment

I faced with code, which compilation result was surprised for me. public class Test3{ public static Map map(){return new HashMap();} } class A{ static void f(Map bcMap){} public static void…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
5
votes
0 answers

Using do.call for generic S4 method

I have a list of parameters and would like to call B(var3=list(1:3)) via do.call. But the following example only calls the method for dispatch "missing". How do I access the method for dispatch "ANY"? B.initialize<-function(..., var3=list()){ …
Klaus
  • 1,946
  • 3
  • 19
  • 34
5
votes
2 answers

Generic methods with constraints that are generic

What I want to do is have a method that takes a generic type as a parameter with a constraint. However, the constraint's type also has a second generic type, but I want the method to work regardless of what the second typing is: public class…
Kyle Baran
  • 1,793
  • 2
  • 15
  • 30
5
votes
2 answers

Linking to an S4 method in a .rd file?

I'm writing a package with an S4 class, and I've written methods for as.POSIXct and as.POSIXlt for the class. I've written the documentation and everything looks fine, except that I would like to reference the as.POSIXct method in the documentation…
Eli Sander
  • 1,228
  • 1
  • 13
  • 29
1
2
3
11 12