type-inference

What rules are used for type inference when using Generics

Does anyone know where I can find a discussion on how type inference works when using Generics in Java. ...

Strange ClassCastException

This: Timerange longest = Timerange.longest(breaks); if (longest.durationInHours() >= MIN_FREE_HOURS) return true; is OK. But this: if (Timerange.longest(breaks).durationInHours() >= MIN_FREE_HOURS) return true; gives: java.lang.ClassCastException Do you know why?! For simplicity: public static final <T extends Timera...

What is a type inferencer?

Does it only exist in statically typed languages? And is it only there when the language is not strongly typed (i.e. does Java have one)? Also, where does it belong - in the compilation phase assuming it's a compiled language? In general, are the rules when the type is ambiguous dictated by the language specification or left up to the i...

How does Haskell know which typeclass instance you mean?

This question arose while reading the new chapter in the excellent Learn You a Haskell about applicative functors. The Applicative typeclass has, as part of the definition for the Maybe instance: pure = Just If I just go to GHCi and import Control.Applicative, and do: pure (3+) I don't get Just anything (makes sense). But if I use...

Why can't F#'s type inference handle this?

I have a sequence of FileInfo, but I only care about their string names, so I want a sequence of string. At first I tried something like this: Seq.map (fun fi -> fi.Name) fis But for some reason, F#'s type inference isn't good enough to allow this, and made me explicitly give a type to "fi": Seq.map (fun (fi : FileInfo) -> fi.Name) f...

Why can't F# infer the type of this statement?

/// I can't do this let max = float n |> sqrt |> int64 |> Math.BigInt /// But this is allowed let max = Math.BigInt(float n |> sqrt |> int64) ...

Delegates with return value type inference (C#)

I'm still new to delegates and I've been playing with the Delegate-based Data Access Layer described in Steven John Metsker's "Design Patterns in C#" book (an excellent read!). It defines the data access delegate like this: public delegate object BorrowReader(IDataReader reader); The result of using this is code that looks like one of...

Class implicit conversions

I know that I can use implicit conversions with a class as follows but is there any way that I can get a instance to return a string without a cast or conversion? public class Fred { public static implicit operator string(Fred fred) { return DateTime.Now.ToLongTimeString(); } } public class Program { static void...

Error handling in Haskell with Either monad

I have a function that checks whether a type is a subtype of another type: st :: Monad m => Map String Type -- ^type environment -> Set (Type, Type) -- ^assumed subtypes -> (Type, Type) -- ^we are checking if lhs <: rhs -> m (Set (Type, Type)) I want to do error handling. I have the following definition: instance Mona...

How to reduce type declarations when using lambda parameters?

Below is a heavily cut down version of some code I have public class DataInfo<T> { public DataInfo(string description, Func<T, object> funcToGetValue) { this.description = description; this.funcToGetValue= funcToGetValue; } public readonly string description; public readonly Func<T, object> funcToGet...

Why should the following snippet be a problem for C#2.0 type inference ?

I am at chapter 9 of Jon Skeet's CSharp in Depth at a section which explains the improvements to type inference in 3.0 There is code snippet on Pg.247 that 'shouldn't compile with 2.0' - However I can't find a reason why it should not. Tried the problem with VS2008 C# Express Edition on a project with target framework as 2.0 - all thre...

Converting Enumeration to Iterator

I have the following implicit conversion for java.util.Enumerations implicit def enumerationIterator[A](e : Enumeration[A]) : Iterator[A] = { new Iterator[A] { def hasNext = e.hasMoreElements def next = e.nextElement def remove = throw new UnsupportedOperationException() } } Unfortunately it doe...

Describe the Damas-Milner type inference in a way that a CS101 student can understand

Hindley-Milner is a type system that is the basis of the type systems of many well known functional programming languages. Damas-Milner is an algorithm that infers (deduces?) types in a Hindley-Milner type system. Wikipedia gives a description of the algorithm which, as far as I can tell, amounts to a single word: "unification." Is ...

Understanding F# Value Restriction Errors

I don't understand how the Value Restriction in F# works. I've read the explanation in the wiki as well as the MSDN documentation. What I don't understand is: Why, for example, this gives me a Value Restriction error (Taken from this question): let toleq (e:float<_>) a b = (abs ( a - b ) ) < e But ths doesn't: let toleq e (a:floa...

Simplest example of need for "unification" in type inference

I'm trying to get my head around how type inference is implemented. In particularly, I don't quite see where/why the heavy lifting of "unification" comes into play. I'll give an example in "pseudo C#" to help clarify: The naive way to do it would be something like this: Suppose you "parse" your program into an expression tree such tha...

Why does F# infer this type?

This is my code: type Cell<'t>(initial : 't) = let mutable v = initial let callbacks = new List<'t -> unit>() member x.register c = callbacks.Add(c) member x.get () = v member x.set v' = if v' <> v then v <- v' for callback in callbacks do callback v' member x.map f = let c...

Why can't Scala infer the type parameter in this example?

Suppose I have two classes, Input and Output, which are designed to be connected to each other. Output produces values of some type, and Input consumes them. class Input[T] { var output: Option[Output[_ <: T]] = None } class Output[T] { var input: Option[Input[_ >: T]] = None } It's okay if an Input and Output pair don't operate o...

Are Infinite Types (aka Recursive Types) not possible in F#?

I was chatting with Sadek Drobi on twitter when be brought up that F# didn't seem to support Infinite Types. It turns out that in C# you can do something along these lines: delegate RecDelegate<T> RecDelegate<T>(T x); However, after some experimentation on both our parts, we determined that the same in F# seems impossible both implic...

What are the limits of type inference?

What are the limits of type inference? Which type systems have no general inference algorithm? ...

Haskell: type inference and function composition

This question was inspired by this answer to another question, indicating that you can remove every occurrence of an element from a list using a function defined as: removeall = filter . (/=) Working it out with pencil and paper from the types of filter, (/=) and (.), the function has a type of removeall :: (Eq a) => a -> [a] -> [a] ...