How can I create a generic array in Java?

I have to ask a question in return: is your GenSet “checked” or “unchecked”? What does that mean? Checked: strong typing. GenSet knows explicitly what type of objects it contains (i.e. its constructor was explicitly called with a Class<E> argument, and methods will throw an exception when they are passed arguments that are not of … Read more

Get Enum Instance from Class

public static <T extends Enum<T>> T getInstance(final String value, final Class<T> enumClass) { return Enum.valueOf(enumClass, value); } And the method is to be used as: final Shape shape = getInstance(“CAT”, Shape.class); Then again, you can always use final Shape shape = Shape.valueOf(“CAT”); which is a shortcut for Enum.valueOf(Shape.class, “CAT”);

Why is it possible to instantiate a struct without the new keyword?

Why are we not forced to instantiate a struct with “new”, like when using a class? When you “new” a reference type, three things happen. First, the memory manager allocates space from long term storage. Second, a reference to that space is passed to the constructor, which initializes the instance. Third, that reference is passed … Read more

Does System.Activator.CreateInstance(T) have performance issues big enough to discourage us from using it casually?

As always, the only correct way to answer a question about performance is to actually measure the code. Here’s a sample LINQPad program that tests: Activator.CreateInstance new T() calling a delegate that calls new T() As always, take the performance program with a grain of salt, there might be bugs here that skews the results. … Read more

Create Annotation instance with defaults, in Java

To create an instance you need to create a class that implements: java.lang.annotation.Annotation and the annotation you want to “simulate” For example: public class MySettings implements Annotation, Settings But you need to pay special attention to the correct implementation of equals and hashCode according to the Annotation interface. http://download.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/Annotation.html If you do not want to … Read more

What exactly is “broken” with Microsoft Visual C++’s two-phase template instantiation?

I’ll just copy an example from my “notebook” int foo(void*); template<typename T> struct S { S() { int i = foo(0); } // A standard-compliant compiler is supposed to // resolve the ‘foo(0)’ call here (i.e. early) and // bind it to ‘foo(void*)’ }; void foo(int); int main() { S<int> s; // VS2005 will resolve … Read more

Call a model method in a Controller

To complete davidb’s answer, two things you’re doing wrong are: 1) you’re calling a model’s function from a controller, when the model function is only defined in the model itself. So you do need to call Project.form_search and define the function with def self.form_search 2) you’re calling params from the model. In the MVC architecture, … Read more

tech