type-inference

Generic types with type parameter in C#

I don't think that this could be done in C#, but posting this just to make sure. Here's my problem. I would like to do something like this in C#: var x = 10; var l = new List<typeof(x)>(); or var x = 10; var t = typeof(x); var l = new List<t>(); But of course this doesn't work. Although this shouldn't be a problem as the type t is...

Why does C# "var" keyword not work when looping through a Visio Masters collection?

I am using the Visio API to loop through each Microsoft.Office.Interop.Visio.Master object in a Microsoft.Office.Interop.Document's Masters collection. When I use var as follows, the compiler only recognizes master as type object and throws an error: foreach (var master in doc.Masters) Instead, I have to do this for it to work: fore...

JSTL foreach and intellisense

With a code like this: <c:forEach items="${customers}" var="customer"> ${customer.name} </c:forEach> IntelliJ Idea is able to infer that the type of the "customer" variable in the ForEach loop is of the class "Customer" (given that Customers is something like List<Customer>). If I refactor the java customer class and change getNam...

Simple Type Inference in Scala

I have been looking at type inference in Scala and there are a couple of things I'd like to understand a bit better around why expression/method-return types have to be explicitly declared in a few cases. Explicit return declaration Example (works if return keyword is ommitted): def upCase(s: String) = { if (s.length == 0) retur...

Why is Haskell throwing a 'cannot construct infinite type' error?

I wrote the following code in Haskell to compute the dot product of two vectors, but cannot compile it due to the following error: cannot construct infinite type: a = [a] When generalising the type(s) for dot' dot :: (Num a) => [a] -> [a] -> a [] `dot` [] = 0 x@[xi,xs] `dot` y@[yi,ys] = xi*yi + (xs `dot` ys) I've taken a look at thi...

Why does Haskell interpret my Num type as an Enum?

I'm trying to compile the following function in Haskell to mimic differentiation of a polynomial whose constants are specified in a numerical list: diff :: (Num a) => [a] -> [a] diff [] = error "Polynomial unspecified" diff coeff = zipWith (*) (tail coeff) [0..] Haskell refuses to compile it, giving me the following reason: Could not...

Custom C++ Preprocessor / Typeful Macros

Having seen the advantages of metaprogramming in Ruby and Python, but being bound to lower-level languages like C++ and C for actual work, I'm thinking of manners by which to combine the two. One instance comes in the simple problem for sorting lists of arbitrary structures/classes. For instance: struct s{ int a; int b; }; vector<s...

Type inference: Using generic methods with implicit type conversion

The problem is that you want to flatMap a List[Option[T]] to a List[T] : val l = List(Some("Hello"), None, Some("World")) to get: List(Hello, World) but there is no nice solution: l flatMap( o => o) l flatMap identity[Option[String]] l flatMap ( x => Option.option2Iterable(identity(x))) for(x <- l; y <- x) yield y The obvious so...

Forcing F# type inference on generics and interfaces to stay loose

We're gettin' hairy here. I've tested a bunch of tree-synchronizing code on concrete representations of data, and now I need to abstract it so that it can run with any source and target that support the right methods. [In practice, this will be sources like Documentum, SQL hierarchies, and filesystems; with destinations like Solr and a c...

Why the tuple type can not be inferred in the list recursion?

I want to refine the raw text by using regular expression, given a list of (patten,replacement) tuple. I tried to use the patten matching on the list element but failed, the error showed that "This expression was expected to have type string * string list but here has type 'a list". How can I fix this problem? Thanks a lot. Codes ar...

What's the cost of "as" compared to QueryInterface in COM or dynamic_cast in C++?

I'm still trying to map my deep and old knowledge from C/C++ to my somewhat more shallow .Net knowledge. Today the time has come to "as" (and implicitly "is" and cast) in C#. My mental model of "as" is that it's a QueryInterface or dynamic_cast (a dynamic_cast with pointer argument, not reference, that is) for C#. My question is two-fol...

Overloading, generic type inference and the 'params' keyword

Hi, I just noticed a strange behavior with overload resolution. Assume that I have the following method : public static void DoSomething<T>(IEnumerable<T> items) { // Whatever // For debugging Console.WriteLine("DoSomething<T>(IEnumerable<T> items)"); } Now, I know that this method will often be called with a small numb...

What causes this Standard-ML type error?

I was trying to make a tail-recursive version of this very simple SML function: fun suffixes [] = [[]] | suffixes (x::xs) = (x::xs) :: suffixes xs; During the course of this, I was using type annotations on the paramaters. The following code shows this, and causes a type error (given below), whereas if I simply remove the type annot...

Conditionally assign C# var: As elegant as it gets?

I understand that the C# keyword var implies the type at compile time and therefore requires the declaration and assignment in the same place, making the following construct illegal: var something; if (condition) { something=1; } else { something = 0; } Even though the C# compiler could principally determine that all assignme...

Is there an equivalent to the C# "var" keyword in C++/CLI ?

In C#, I like the var keyword for situations like this: var myList = new List<MyType>(); Is there any equivalent in C++/CLI, or do I have to repeat the type name everytime just like this: List<MyType ^>^ myList = gcnew List<MyType ^>(); Could not find an explicit statement in the docs or by Google so far. I am using Visual Studio 2...

Haskell -- more type inference problems

I have the following expression: getCount :: (Num a) => a -> [a] getCount int = foldl processOneCount [0,0,0,0,0,0,0,0,0,0] (map (singleDigitCount) (map (digitToInt) (show int))) and i get the following error: Couldn't match expected type `a' against inferred type `Int' `a' is a rigid type variable bound by ...

.NET: Inferred generic types on static methods

Suppose I have public static List<T2> Map<T,T2>(List<T> inputs, Func<T, T2> f) { return inputs.ConvertAll((x) => f(x)); } private int Square(int x) { return x*x; } public void Run() { var inputs = new List<Int32>(new int[]{2,4,8,16,32,64,128,256,512,1024,2048}); // this does not compile var outputs = Map(inputs, Squa...

Type-inferring a constant in C#

In C#, the following type-inference works: var s = "abcd"; But why can't the type be inferred when the variable is a constant? The following throws a compile-time exception: const var s = "abcd"; // <= Compile time error: // Implicitly-typed local variables cannot be constant ...

Can GHCi tell me the type of a local Haskell function?

Is it possible to query the ghci for the type it inferred for a function inside another function? ...

Type Inference on Method Return Type

Why does Scala fail to infer the return type of the method when there's explicit return statement used in the method? For instance, why does the following code compile : object Main { def who = 5 def main(args: Array[String]) = println(who) } ...but the following doesn't : object Main { def who = return 5 def main(args: Arra...