rsdn / nemerle

Nemerle language. Main repository.
http://nemerle.org
Other
620 stars 89 forks source link

type inference or type conversion issue #164

Open ghost opened 12 years ago

ghost commented 12 years ago
using System;
{
    static class Program
    {      
        static void Main(string[] args)
        {
            //this should compile but got this error:           
            //...\Program.cs(15,34): error : expected int, got long in array initializer: System.Int64 is not a subtype of System.Int32 [simple require]
            //...\Program.cs(15,28): error : expected array[long], got array[int] in type-enforced expression: the types long and int are not compatible [simple unify]

            //long[] Intervals = new[] {1, 10, 100, 1000, long.MaxValue };

            int i = 1000;
            long l = 1000L;
            var expectLongButGetInt = (1000L * i);
            var expectLongIndeedGetLong = (l * i);
            System.Console.WriteLine(expectLongButGetInt.GetType().ToString());   //print System.Int32
            System.Console.WriteLine(expectLongIndeedGetLong.GetType().ToString());   //Print System.Int64
            System.Console.Read();        
        }
}
NN--- commented 12 years ago

It happens because Nemerle understands array type by the first element. Unlike C# where it tries to calculate the most common type.

Workaround:

var Intervals = new[] { 1L, 10, 100, 100, long.MaxValue };