nemerle icon indicating copy to clipboard operation
nemerle copied to clipboard

Type inference fails in linq expression

Open Stitchous opened this issue 12 years ago • 2 comments

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]

Stitchous avatar Jul 04 '13 14:07 Stitchous

Nemerle infer type of ptr as int. Use this pattern instead:

def intPtrs = Enumerable.Empty().Select(ptr => ptr : IntPtr + 5);

VladD2 avatar Jul 04 '13 16:07 VladD2

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.

NN--- avatar Jul 04 '13 19:07 NN---