nemerle
nemerle copied to clipboard
Type inference fails in linq expression
this code
using System;
using System.Linq;
module Program
{
Main() : void
{
def intPtrs = Enumerable.Empty.[IntPtr]().Select(ptr => ptr + 5);
}
}
doesn't compile and produces following error messages:
each overload has an error during call:
overload #1, "method System.Linq.Enumerable.Select(source : System.Collections.Generic.IEnumerable[TSource], selector : System.Func[TSource, int, TResult]) : System.Collections.Generic.IEnumerable[TResult]" fail because:
in argument #2 (selector), needed a System.Func[System.IntPtr, int, ?], got int+ -> int-: int+ -> int- is not a subtype of System.Func[System.IntPtr, int, object+] [simple require]
overload #2, "method System.Linq.Enumerable.Select(source : System.Collections.Generic.IEnumerable[TSource], selector : System.Func[TSource, TResult]) : System.Collections.Generic.IEnumerable[TResult]" fail because:
in argument #2 (selector), needed a System.Func[System.IntPtr, ?], got int+ -> int-: int+ -> int- is not a subtype of System.Func[System.IntPtr, object+] [simple require]
Nemerle infer type of ptr as int. Use this pattern instead:
def intPtrs = Enumerable.Empty().Select(ptr => ptr : IntPtr + 5);
There is problem. Same code does work in C# !
using System;
using System.Collections.Generic;
module Enumerable
{
public Empty[T]() : IEnumerable[T] { [] }
public Select[T](this x : IEnumerable[T], _f : Func[T, T]) : IEnumerable[T] { x }
}
class MyPtr
{
public static @+(l : MyPtr, r : int) : MyPtr { null }
}
module Program
{
Main() : void
{
def intPtrs = Enumerable.Empty.[MyPtr]().Select(ptr => ptr + 5);
}
}
Compiler doesn't check for exitance of '+' in MyPtr class.