Skip to content

System::Char binds to double/float via to_f (0.0) but to decimal as its code point #153

Closed

Description

@Largo

A System::Char passed to a numeric CLR parameter is converted three different ways depending on the parameter type:

parameter System::Char.new('a') System::Char.new('5') System::Char.MaxValue
int TypeError TypeError TypeError
double, float 0.0 5.0 0.0
decimal 97 53 65535
  • int follows the rule in Converter.Generated.cs ("System::Char is treated like a single-character string and not a numeric type"): no conversion.
  • double/float go through ConvertToFloatingPointAction: no implicit conversion applies, so the protocol calls Char#to_f, the lenient String#to_f. A non-digit char silently becomes 0.0. A Ruby String passed to the same parameter goes through the strict Float() parse and raises ArgumentError for 'a'.
  • decimal has no Ruby protocol conversion, so the DLR's numeric char→decimal conversion applies and gives the code point.

The old Tests/Interop/net/method/binding/numeric_and_string_spec.rb expected the code point for Double, Single and Decimal (and no Int32 binding), and tagged Double/Single as failing in 2009. They still fail.

Pick one rule. Either Char is numeric for floating-point and decimal parameters, as in C#, or it is string-like everywhere: TypeError, or the strict Float() parse for floating-point, the same as a String. Then make all three agree. Silently getting 0.0 is the worst of the options.

Found while porting the binding matrices for #144; ClrInterop_BindingNumericAndString1 leaves these rows out and names this issue.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions