type-inference

C# Generic method type argument inference

Is there any way that I can generalise the type definitions here? Ideally, I'd like to be able to change the type of 'testInput' and have test correctly infer the type at compile time. public static void Run() { var testInput = 3; var test = ((Func<int, int>) Identity).Compose<int,int,int>(n => n)(testInput); Console.WriteLi...

problems with Haskell's Number Types

I have the following haskell code: fac n = product [1..n] taylor3s w0 f f' f'' t h = w1 : taylor3s w1 f f' f'' (t+h) h where hp i = h^i / fac i w1 = w0 + (hp 1) * f t w0 + (hp 2) * f' t w0 + (hp 3) * f'' t w0 taylor_results = take 4 $ taylor3s 1 f f' f'' 1 0.25 where f t x = t^4 - 4*x/t f' t x = 4*t^3 - 4*(f t x...

C#: at design time, how can I reliably determine the type of a variable that is declared using var?

I'm working on a completion (intellisense) facility for C# in emacs. The idea is, if a user types a fragment, then asks for completion via a particular keystroke combination, the completion facility will use .NET reflection to determine the possible completions. Doing this requires that the type of the thing being completed, be know...

Partial generic type inference possible in C#?

I am working on rewriting my fluent interface for my IoC class library, and when I refactored some code in order to share some common functionality through a base class, I hit upon a snag. Note: This is something I want to do, not something I have to do. If I have to make do with a different syntax, I will, but if anyone has an idea on ...

Scala type inference question

I was just pottering about with Tony Morris' excellent exercise on catamorphisms, when I was pondering what was happening in the following situation... def cata[X](some: A => X, none: => X): X Let me now call this method as follows: def isDefined: Boolean = cata( _ => true, false) I was wondering whether the type inferencer determi...

Why generic type inference doesn't work in that case ?

When trying to compile the following code in LINQPad : void Main() { DriveInfo.GetDrives().Select(GetProviderName).Dump(); } static string GetProviderName(DriveInfo drive) { // some irrelevant WMI code... } I get the following error : The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic....

Java Generics Class Parameter Type Inference

Given the interface: public interface BasedOnOther<T, U extends BasedList<T>> { public T getOther(); public void staticStatisfied(final U list); } The BasedOnOther<T, U extends BasedList<T>> looks very ugly in my use-cases. It is because the T type parameter is already defined in the BasedList<T> part, so the "uglyness" com...

decltype and parenthesis

I don't understand the last line of the example on page 148 of the FCD (§7.6.1.2/4): const int&& foo(); int i; struct A { double x; }; const A* a = new A(); decltype(foo()) x1 = i; // type is const int&& decltype(i) x2; // type is int decltype(a->x) x3; // type is double decltype((a->x)) x4 = x3; // type is co...

infer a common supertype based on a parameter value and function parameter types

Should the following be compiled without needing an explicit type definition on this? def prepList[B >: A](prefix: PlayList[B]) : PlayList[B] = prefix.foldr(this: PlayList[B])((node, suffix) => suffix.prepNode(node)) It seems to me that the type should be able to inferred. Is this just a limitation in the Scala compiler, or is there...

Class type inference from static method / caller?

Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) { // Do stuff with xyz... } } public class Ordering<TEntity> where TEntity : class { private readonly Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> Transform; private Ordering(Func<IQueryable<TEntity>,...

Returning address of local variable or temporary when using C++0x decltype return value

Edit: This is indeed a bug in the compiler, I've opened a defect and got the following response. Hello Motti, Thank you for submitting this issue. As noted in the stackoverflow posting, this is a bug in our decltype implementation. Unfortunately, we cannot fix this bug in the next release of Visual Studio since the code is relativ...

Why is F#'s type inference so fickle?

The F# compiler appears to perform type inference in a (fairly) strict top-to-bottom, left-to-right fashion. This means you must do things like put all definitions before their use, order of file compilation is significant, and you tend to need to rearrange stuff (via |> or what have you) to avoid having explicit type annotations. How h...

Designing a convenient default valued map in Scala

I find myself using a lot of nested maps, e.g a Map[Int, Map[String, Set[String]]], and I'd like to have new Maps, Sets, etc. created automatically when I access a new key. E.g. something like the following: val m = ... m(1992)("foo") += "bar" Note that I don't want to use getOrElseUpdate here if I don't have to because it gets pretty...

How to make template type inference work in this case?

Once again, I wish C++ had stronger typedefs: #include <vector> template<typename T> struct A { typedef std::vector<T> List; }; template<typename T> void processList(typename A<T>::List list) { // ... } int main() { A<int>::List list; processList<int>(list); // This works. processList(list); // This doesn't. ...

Generic methods in .NET cannot have their return types inferred. Why?

Given: static TDest Gimme<TSource,TDest>(TSource source) { return default(TDest); } Why can't I do: string dest = Gimme(5); without getting the compiler error: error CS0411: The type arguments for method 'Whatever.Gimme<TSource,TDest>(TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly....

Why 'Code is not sufficiently generic'?

Can anyone explain why the second example below won't compile? 'Test 2' gives "error FS0670: This code is not sufficiently generic. The type variable ^a could not be generalized because it would escape its scope.". I fail to understand this error message. // Test 1 type test1<'a> = | A of 'a with override t.ToString() = ...

Implicit type parameters in Haskell class definition?

It normally seems the following is illegal: class Foo a where foo :: a -> b -> a Which makes sense; how do we know what b is? However, if we look at Functor's definition: class Functor f where fmap :: (a -> b) -> f a -> f b we see a and b showing up even though we only specify f as a type variable. I'm guessing this is all...

In C#, how do inferred variables using var behave with respect to memory and lifecycle?

Ok, I've been reading everything I can find on var, and I'm pretty sure I've got a handle on their basic behaviors and type inference. I've always been a big proponent of explicit types, so var makes me a little edgy when I'm reading code. So I have a couple of questions regarding its memory usage and the behavior of a variable declared ...

Why is [String] the inferred type when there are no lists visible in the code?

I have this test: testReadFile = runTestTT $ TestLabel "InteractionDomain.readFileContentsToList" (TestList [ (TestLabel "testing infrastructure: read test file" (TestList [ TestCase (withTempFileContainingText (\fileHandle -> assertEqual "contents as expected" "The dog barks at midnight." (do co...

Under what conditions is inferring Nothing desirable?

In my own code, and on numerous mailing list postings, I've noticed confusion due to Nothing being inferred as the least upper bound of two other types. The answer may be obvious to you*, but I'm lazy, so I'm asking you*: Under what conditions is inferring Nothing in this way the most desirable outcome? Would it make sense to hav...