Why a generic can’t be assigned to another even if their type arguments can?

Instantiating a generic type with different type arguments produces two new different named types. Note that every time you supply a type argument, including in function arguments or return types, you are instantiating the generic type: // Props is instantiated with type argument ‘Generic’ func Problem() Props[Generic] { return ExampleProps } Therefore Props[Example] is just … Read more

Pattern for lazy thread-safe singleton instantiation in java

the lazy thread-safe singleton instantion is kinda not easy to understand to every coder No, it’s actually very, very easy: public class Singleton{ private final static Singleton instance = new Singleton(); private Singleton(){ … } public static Singleton getInstance(){ return instance; } } Better yet, make it an enum: public enum Singleton{ INSTANCE; private Singleton(){ … 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

How to instantiate an object with a private constructor in C#?

You can use one of the overloads of Activator.CreateInstance to do this: Activator.CreateInstance(Type type, bool nonPublic) Use true for the nonPublic argument. Because true matches a public or non-public default constructor; and false matches only a public default constructor. For example: class Program { public static void Main(string[] args) { Type type=typeof(Foo); Foo f=(Foo)Activator.CreateInstance(type,true); } … Read more

C# Delegate Instantiation vs. Just Passing the Method Reference [duplicate]

As long as the method group SomeObject.SomeMethod has a method with return type void and taking no parameters there is no difference. This is because ThreadStart is defined as a delegate that returns void and takes no parameters and therefore there is an implicit conversion from the method group SomeObject.SomeMethod to ThreadStart. Thus, both are … Read more

C++ Object Instantiation

On the contrary, you should always prefer stack allocations, to the extent that as a rule of thumb, you should never have new/delete in your user code. As you say, when the variable is declared on the stack, its destructor is automatically called when it goes out of scope, which is your main tool for … Read more

Why is __init__() always called after __new__() in Python?

Use __new__ when you need to control the creation of a new instance. Use __init__ when you need to control initialization of a new instance. __new__ is the first step of instance creation. It’s called first, and is responsible for returning a new instance of your class. In contrast, __init__ doesn’t return anything; it’s only … Read more