Fable icon indicating copy to clipboard operation
Fable copied to clipboard

[Python] Boxing bytes looses type information

Open HLWeil opened this issue 1 year ago • 3 comments

Description

When boxing a byte value, it looses its underlying type information.

Repro code

test.fsx

let intValue = box 1
let byteValue = box 2uy

let matchType (v : obj) =
    match v with
    | :?int as i -> printfn "int: %d" i
    | :?byte as u -> printfn "byte: %u" u
    | :?uint8 as u -> printfn "uint8: %u" u // equals byte, just for testing against Fable
    | _ -> printfn "other"

matchType intValue // int: 1
matchType byteValue // other

printfn $"{2uy.GetType().Name}" // Byte
printfn $"{byteValue.GetType().Name}" // Object

test.cmd

dotnet fable . --lang python
python test.py

Expected and actual results

Expected result: Boxed byte value should match the reflective match case, and calling GetType().Name should return "Byte"

Actual result: Match case is not hit and "Object" is returned

Related information

  • dotnet fable --version: 4.13.0
  • dotnet tool list/update/install: 4.13.0
  • Windows11

@freymaurer

HLWeil avatar Feb 22 '24 09:02 HLWeil

@HLWeil @Freymaurer, this is a bit tricky since Python really just have int so we need to do some tricks if we want to make this work. Another possibility is that we (Fable) take a dependency on NumPy to have sized and signed integers. Would NumPy as a dependency be acceptable for your application?

dbrattli avatar Mar 10 '24 09:03 dbrattli

Same thing with JS/TS, there is limited RTTI cause the base type for all integer types is either number (i.e. float64) or bigint.

ncave avatar Mar 12 '24 01:03 ncave

Yes, with Python I'm considering removing the int subclasses which would make all integers transpile to variable size int and instead use numpy if more specific integers and reflection handling is needed. That should fix most array problems as well, i.e use list for arrays or numpy arrays if available.

dbrattli avatar Mar 12 '24 08:03 dbrattli