Type aliases for Java generics

If you want full type safety, I don’t think you can do better without some kind of wrapper classes. But, why not make those classes inherit/implement the original generic versions, like this: public class FooBBQ extends Foo<Bar<Baz,Qux>> { … } This eliminates the need for toGeneric() method, and it is more clear, in my opinion, … Read more

C# ‘is’ type check on struct – odd .NET 4.0 x86 optimization behavior

I worked up a similar example that fails the same way: using System; using System.Runtime.CompilerServices; public class Program { static void Main() { Console.Write(Verify(Test.Create())); Console.ReadLine(); } //[MethodImpl(MethodImplOptions.NoInlining)] static bool Verify(IDisposable item) { return item is Test; } struct Test : IDisposable { public void Dispose() { } public static Test Create() { return new Test(); … Read more

casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

For me (using Java 1.6.0_26), the first snippet gives the same exception as the second one. The reason is that the Arrays.asList(..) method does only return a List, not necessarily an ArrayList. Because you don’t really know what kind (or implementation of) of List that method returns, your cast to ArrayList<String> is not safe. The … Read more

No function matches the given name and argument types

Your function has a couple of smallint parameters. But in the call, you are using numeric literals that are presumed to be type integer. A string literal or string constant (‘123’) is not typed immediately. It remains type “unknown” until assigned or cast explicitly. However, a numeric literal or numeric constant is typed immediately. The … Read more

What exactly is or was the purpose of C++ function-style casts?

Function style casts bring consistency to primitive and user defined types. This is very useful when defining templates. For example, take this very silly example: template<typename T, typename U> T silly_cast(U const &u) { return T(u); } My silly_cast function will work for primitive types, because it’s a function-style cast. It will also work for … Read more

What does typing.cast do in Python?

From the documentation (emphasis mine): Cast a value to a type. This returns the value unchanged. To the type checker this signals that the return value has the designated type, but at runtime we intentionally don’t check anything (we want this to be as fast as possible). The “casting” only takes place in the type-checking … Read more