consulo / consulo-csharp

Languages: C#
Apache License 2.0
50 stars 6 forks source link

Implicit cast problem, from short -> int etc, with primitive wrapper #584

Closed VISTALL closed 2 years ago

VISTALL commented 2 years ago
using System;

public struct ShortWrapper
{
    public short value;

    public ShortWrapper(short va)
    {
        this.value = va;
    }

    public static implicit operator short(ShortWrapper d) => d.value;
    public static implicit operator ShortWrapper(short d) => new ShortWrapper(d);

}

public class Program
{
    public static void Main(string[] args)
    {
        ShortWrapper w = 1;

        int tw = 20;
        int value = tw - w; // error no operator

        test(tw, w); // ShortWrapper can't not be cast to int

        Console.WriteLine(w);
    }

    private static void test(int b, int a)
    {
    }
}

two error in consulo, but compiled