Skip to content

Java generic type constraints translated incorrectly #16

Open

Description

@paulirwin

Input:

public class Foo {
    public static <T extends number> boolean isNegative(T number) {
        return number.longValue() < 0;
    }
}

Output (incorrect):

public class Foo
{
    public static bool IsNegative<T extends number>(T number)
    {
        return number.LongValue() < 0;
    }
}

Output (expected; syntactically correct but might be wrong):

public class Foo
{
    public static bool IsNegative<T>(T number)
    {
        return number.LongValue() < 0;
    }
}

Note that in this case there is no C# equivalent of constraining a generic type parameter to be a number, but at least we can make it syntactically correct.

Activity

  1. OJacot-Descombes commented on Jun 30, 2021

    @OJacot-Descombes
    Contributor

    Mads Torgerson an his team are currently working on allowing static abstract members in interfaces. The motivation is to allow operators in interfaces. So, a future version of C# might well have a INumeric<T> interface defined on numeric types.

  2. self-assigned this
    on Jul 4, 2021
  3. paulirwin commented on Jul 4, 2021

    @paulirwin
    OwnerAuthor

    After fixing #26, I'm now turning my attention to type constraints. I'm thinking that TypeHelper.ConvertType could return an object instead of string, that would include a list of type parameters with their constraints/bounds. A generic type or method definition could then add these as type constraints with the where clause. Might be a little ambitious, but I'll see what I can do :-)

  4. OJacot-Descombes commented on Apr 2, 2023

    @OJacot-Descombes
    Contributor

    C# 11 now features Static abstract members in interfaces and the INumber Interface has been added to .NET 7.

  5. maximilien-noal commented on Sep 5, 2023

    @maximilien-noal
    Contributor

    I tried to use INumber as a generic constraint, but it's a INumber<TSelf> interface in fact, so it defeats the purpose.

    There is no non-generic version of the INumber interface.

    You could use where T : unmanaged which would only accept pure value types (enums, pointers, numbers, and user defined value types which do not contain reference types), but that's not a strict equivalent.

  6. maximilien-noal commented on Sep 8, 2023

    @maximilien-noal
    Contributor

    The proper translation would be to implement non-generic overloads for each number type (int, nint, Half, Double, Float, sbyte, short, long, and their unsigned variants)... instead of a generic method.

  7. paulirwin commented on Sep 8, 2023

    @paulirwin
    OwnerAuthor

    The proper translation would be to implement non-generic overloads for each number type (int, nint, Half, Double, Float, sbyte, short, long, and their unsigned variants)... instead of a generic method.

    Yeah... we're not going to do that right now 😄

    I just confirmed that INumber<T> should work in this case, the only problem is that the 0 literal isn't supported for that comparison, you have to use T.Zero:

    public static bool IsNegative<T>(T number) where T : INumber<T> => number < T.Zero;

    I don't know if we will get to that level of translation for the 0 literal, but at least theoretically it is possible to use INumber<T> here.

  8. maximilien-noal commented on Sep 8, 2023

    @maximilien-noal
    Contributor

    I'd be curious to see how you can make it work with INumber. Since it's self referencing (for example in the byte class it implements INumber<byte> ) , I was under the impression after some tests that a generic method for all numeric types wasn't possible.

  9. paulirwin commented on Sep 8, 2023

    @paulirwin
    OwnerAuthor

    To make it work, the self-referencing type parameter (CRTP-style) needs to reference T, not the specific types.

    So in my example above (which I confirmed works in .NET 7), IsNegative<T>(T number) where T : INumber<T> becomes reified as IsNegative(byte number) since byte implements INumber<byte>.

    The full method then gets reified as if it were:

    public static bool IsNegative(byte number) => number < byte.Zero;

    That pseudocode directly above doesn't exactly work as is, since the Zero property is explicitly implemented, but it does work as a generic type parameter.

    (Edit: also, of course, this method would always return false 😄 )

  10. maximilien-noal commented on Sep 8, 2023

    @maximilien-noal
    Contributor

    It was so simple ! This will help a lot. Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions