Scala: How to define “generic” function parameters?

Haskell uses Hindley-Milner type inference algorithm whereas Scala, in order to support Object Oriented side of things, had to forgo using it for now. In order to write an add function for all applicable types easily, you will need to use Scala 2.8.0: Welcome to Scala version 2.8.0.r18189-b20090702020221 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15). … Read more

Strange Swift numbers type casting

Yes, I also found this quite surprising. Double conforms to both FloatLiteralConvertible and IntegerLiteralConvertible (ExpressibleByFloatLiteral and ExpressibleByIntegerLiteral in Swift 3). Therefore a Double can be initialized with floating point literal let a = 3.0 or with an integer literal: let b : Double = 10 (The same is true for other floating point types like … Read more

decltype and parentheses

Just above that example, it says if e is an unparenthesized id-expression or a class member access (5.2.5), decltype(e) is the type of the entity named by e. if e is an lvalue, decltype(e) is T&, where T is the type of e; I think decltype(a->x) is an example of the “class member access” and … Read more

Is it possible to emulate template?

After your update: no. There is no such functionality in C++. The closest is macros: #define AUTO_ARG(x) decltype(x), x f.bar<AUTO_ARG(5)>(); f.bar<AUTO_ARG(&Baz::bang)>(); Sounds like you want a generator: template <typename T> struct foo { foo(const T&) {} // do whatever }; template <typename T> foo<T> make_foo(const T& x) { return foo<T>(x); } Now instead of spelling … Read more

Why must I provide explicitly generic parameter types While the compiler should infer the type?

Inference doesn’t consider the return type; you can, however, try splitting the generics; for example, you could write code to allow: .Cast().To<Type2>() by having (untested; indicative only) public static CastHelper<T> Cast<T>(this T obj) { return new CastHelper<T>(obj); } public struct CastHelper<TFrom> { private readonly TFrom obj; public CastHelper(TFrom obj) { this.obj = obj;} public TTo … Read more