TypeScript Partial type without undefined

TS 4.4 UPDATE: TS4.4 will have an –exactOptionalPropertyTypes compiler flag to give you the behavior you’re looking for directly with Partial, as long as you intentionally add undefined where you’d like to allow it: interface MyType { foo: string bar?: number | undefined // <– you want this } const merge = (value1: MyType, value2: … Read more

Operator ‘?’ cannot be applied to operand of type ‘T’

Since not everything can be null, you have to narrow down T to be something nullable (aka an object). Structs can’t be null, and neither can enums. Adding a where on class does fix the issue: public abstract class Feature<T> where T : class So why doesn’t it just work? Invoke() yields T. If GetValue … Read more

Which type safety would have lost, had Generics supported the sub-typing? [duplicate]

List<Long> listLong = new ArrayList<Long>(); List<Number> listNumbers = listLong; So, listNumbers and listLong would be two references to the same list, if that was possible, right? listNumbers.add(Double.valueOf(1.23)); So, you would be able to add a Double to that list. listLong, of type List<Long>, would thus contain a Double. The type-safety would thus be broken.

What is a “Regular Type” in the context of move semantics?

Summary: For C++11 I would include: move-ctor (noexcept) move-assign (noexcept) total ordering (operator<() for natural total order and std::less<> if a natural total order does not exist). hash<> And would remove: swap() (non-throwing) – replaced by move operations. Commentary Alex revisits the concept of a regular type in Elements of Programming. In fact, much of … Read more

TStringList vs. TList

TStringList is a descendant of TStrings. TStringList knows how to sort itself alphabetically. TStringList has an Objects property. TStringList doesn’t make your code incompatible with all previous versions of Delphi. TStringList can be used as a published property. (A bug prevents generic classes from being published, for now.)

C# Generic Interface and Factory Pattern

You should be able to do something like this: public static class GenericFactory { public static IGeneric<T> CreateGeneric<T>() { if (typeof(T) == typeof(string)) { return (IGeneric<T>) new GenericString(); } if (typeof(T) == typeof(int)) { return (IGeneric<T>) new GenericInt(); } throw new InvalidOperationException(); } } You would use it like this: var a = GenericFactory.CreateGeneric<string>(); var … Read more

Understanding C# generics much better

A very simple example is the generic List<T> class. It can hold a number of objects of any type. For example, you can declare a list of strings (new List<string>()) or a list of Animals (new List<Animal>()), because it is generic. What if you couldn’t use generics? You could use the ArrayList class, but the … Read more

Anonymous Types – Are there any distingushing characteristics?

EDIT: The list below applies to C# anonymous types. VB.NET has different rules – in particular, it can generate mutable anonymous types (and does by default). Jared has pointed out in the comment that the naming style is different, too. Basically this is all pretty fragile… You can’t identify it in a generic constraint, but: … Read more

Why use generic constraints in C#

You ask, “can’t I switch ALL references of T in this class with ISomething?” So I think you mean to compare: public class MyClass<T> where T : ISomething { public T MyProperty { get; set; } } With: public class MyClass { public ISomething MyProperty { get; set; } } In the second example, MyProperty … Read more

Java: Generic method for Enums

public static <E extends Enum<E>> String getEnumString(Class<E> clazz, String s){ for(E en : EnumSet.allOf(clazz)){ if(en.name().equalsIgnoreCase(s)){ return en.name(); } } return null; } The original has a few problems: It accepts an instance of the enum instead of the class representing the enum which your question suggests you want to use. The type parameter isn’t used. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)