type-inference

Use of var keyword in C#

After discussion with colleagues regarding the use of the 'var' keyword in C# 3 I wondered what people's opinions were on the appropriate uses of type inference via var? For example I rather lazily used var in questionable circumstances, e.g.:- foreach(var item in someList) { // ... } // Type of 'item' not clear. var something = someOb...

Fundeps and GADTs: When is type checking decidable?

I was reading a research paper about Haskell and how HList is implemented and wondering when the techniques described are and are not decidable for the type checker. Also, because you can do similar things with GADTs, I was wondering if GADT type checking is always decidable. I would prefer citations if you have them so I can read/unde...

Make Test.QuickCheck.Batch use a default type for testing list functions

I am testing a function called extractions that operates over any list. extractions :: [a] -> [(a,[a])] extractions [] = [] extractions l = extract l [] where extract [] _ = [] extract (x:xs) prev = (x, prev++xs) : extract xs (x : prev) I want to test it, for example, with import Test.QuickCheck.Batch prop_len l = l...

How to infer coercions?

I would like to know how to infer coercions (a.k.a. implicit conversions) during type inference. I am using the type inference scheme described in Top Quality Type Error Messages by Bastiaan Heeren, but I'd assume that the general idea is probably the same in all Hindley-Milner-esque approaches. It seems like coercion could be treated a...

Expression inference during inheritance

I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program { public static void Main() { Descendant d = new Descendant(); d.TestMethod(); } } public class Base { protected void FigureItOut<TClass, TMember>(Expression<Func<TClass, TMember>> expr) {...

C# 3.0 Func/OrderBy type inference

So odd situation that I ran into today with OrderBy: Func<SomeClass, int> orderByNumber = currentClass => currentClass.SomeNumber; Then: someCollection.OrderBy(orderByNumber); This is fine, but I was going to create a method instead because it might be usable somewhere else other than an orderBy. private int ReturnNumber(So...

Java: Collections.emptyList() returns a List<Object>?

I'm having some trouble navigating Java's rule for inferring generic type parameters. Consider the following class, which has an optional list parameter: import java.util.Collections; import java.util.List; public class Person { private String name; private List<String> nicknames; public Person(String name) { this(name,Coll...

Type inference to unification problem

Has anyone an idea how the type inference problem E > hd (cons 1 nil) : α0 with the typing environment E={ hd : list(α1 ) → α1 , cons : α2 → list(α2 ) → list(α2 ), nil : list(α3 ), 1 : int } can be transferred in an unificat...

Are there well known algorithms for deducing the "return types" of parser rules?

Given a grammar and the attached action code, are there any standard solution for deducing what type each production needs to result in (and consequently, what type the invoking production should expect to get from it)? I'm thinking of an OO program and action code that employs something like c#'s var syntax (but I'm not looking for som...

C# 3.0 generic type inference - passing a delegate as a function parameter

I am wondering why the C# 3.0 compiler is unable to infer the type of a method when it is passed as a parameter to a generic function when it can implicitly create a delegate for the same method. Here is an example: using System; class Test { static void foo(int x) { } static void bar<T>(Action<T> f) { } static void test(...

implementing type inference

well I see some interesting discussions here about static vs. dynamic typing I generally prefer static typing, due to compile type checking, better documented code,etc. However I do agree that they do clutter up the code if done the way Java does it, for example. so Im about to start building a language of my own and type inference is ...

Can I declare a Global Inferred variable in C#?

I need to declare the query variable outside the switch statement that way I would only have one variable that would handle different result of the LINQ Query. Please see the code below. Problem here is that I cannot infer a variable without initializing it var query; Switch(filter) { case 1: var query = from c in Customers ...

When to exploit type inference in Haskell?

I'm curious as to how often experienced Haskell programmers really use type inference in practice. I often see it praised as an advantage over the always-explicit declarations needed in certain other languages, but for some reason (perhaps just because I'm new) it "feels" right to write a type signature just about all the time... and I'm...

How to pass an array size as a template with template type?

My compiler behaves oddly when I try to pass a fixed-size array to a template function. The code looks as follows: #include <algorithm> #include <iostream> #include <iterator> template <typename TSize, TSize N> void f(TSize (& array)[N]) { std::copy(array, array + N, std::ostream_iterator<TSize>(std::cout, " ")); std::cout << s...

How good is the C# type inference?

Title says it all, how good is C# type inference? I read somewhere that it's only for local variables? Does it work for class level attributes? For method signatures? Method return types? etc. ...

C# type inference of a generic method type parameter where the method has no arguments

Given the following generic interface and implementing class: public interface IRepository<T> { // U has to be of type T of a subtype of T IQueryable<U> Find<U>() where U : T; } public class PersonRepository : IRepository<Employee> { } How could I call the Find method without specififying U? var repository = new EmployeeRe...

F# functions with generic parameter types

Hello, I am trying to figure out how to define a function that works on multiple types of parameters (e.g. int and int64). As I understand it, function overloading is not possible in F# (certainly the compiler complains). Take for example the following function. let sqrt_int = function | n:int -> int (sqrt (float n)) | n:int6...

C# type inference : fails where it shouldn't?

Notice the following code. The offending line has been commented out. interface I<R> { } class C : I<int> { } class Program { private static void function<T, R>(T t) where T : class, I<R> { } static void Main(string[] args) { // function(new C()); // wont compile function<C, int>(new C()); } } ...

Generic identity function for use with type inference

Hi I was wondering if it is possible, as my 5 minutes of experimentation proved fruitless. I hoped it would be as easy as: T Identity<T>(T t) { return t; } But this fails to compile on generic methods taking Func parameters. Eg OrderBy. Even specifying type parameters (which is exactly what I want to avoid!), it fails to compile. ...

Type parameters versus member types in Scala

I'd like to know how do the member types work in Scala, and how should I associate types. One approach is to make the associated type a type parameter. The advantages of this approach is that I can prescribe the variance of the type, and I can be sure that a subtype doesn't change the type. The disadvantages are, that I cannot infer the...