New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the type of a number fails most of the times #963

Closed
teo-tsirpanis opened this Issue Jun 16, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@teo-tsirpanis

teo-tsirpanis commented Jun 16, 2018

If one tries to convert a number to another type, Websharper fails with an error like this one:

WebSharper error FS9001: Method name not found in JavaScript compilation: (ToByte<_> : 'T0 -> System.Byte), Members: op_Dereference, op_Modulus, op_BitwiseAnd, op_Multiply, op_Exponentiation, PowInteger, op_Addition, op_Subtraction, op_Division, op_ColonEquals, op_LeftShift, op_Equality, op_Inequality, op_LessThan, op_GreaterThan, op_LessThanOrEqual, op_GreaterThanOrEqual, op_RightShift, op_Concatenate, op_ExclusiveOr, op_BitwiseOr, op_UnaryPlus, op_UnaryNegation, op_LogicalNot, Abs, Acos, Asin, Atan, Atan2, Box, Ceiling, ToChar, Cos, Cosh, Decrement, Enum, ToDecimal, ToDouble, Exp, ToFloat, Floor, Fst, Identity, Ignore, Increment, Infinity, ToInt, ToSingle, ToInt32, ToEnum, ToInt64, Log, Log10, InfinitySingle, NaNSingle, NaN, Not, Raise, Ref, Round, CreateSequence, Sin, Sinh, Snd, Sqrt, ToString, Tan, Tanh, Unbox, IsNull, Hash, op_PipeRight, Compare, DefaultArg, Min, op_ComposeLeft, op_PipeLeft, op_PipeLeft2, op_PipeLeft3, op_ComposeRight, op_Append, op_PipeRight2, op_PipeRight3, get_Infinity, Max, get_InfinitySingle, get_NaNSingle, get_NaN, Using, op_Range, op_RangeStep, KeyValuePattern, Truncate, Sign, Pown, InvalidArg, InvalidOp, FailWith [C:\code\wsBug\wsBug.fsproj]

Code sample:

namespace Issue963

open WebSharper

[<JavaScript>]
type Class1() = 
    member __.Int8 = 45 |> int8
    member __.UInt8 = 133 |> byte
    member __.Int16 = 196 |> uint16
    member __.UInt16 = 198 |> uint32
    member __.Int32 = 241u |> int // no error here
    member __.UInt32 = 478 |> uint32
    member __.Int64 = 488 |> int64 // no error here
    member __.UInt64 = 497 |> uint64
    member __.Single = 617 |> single
    member __.Double = 647 |> double // no error here
    member __.Decimal = 801 |> decimal // it fails, but because decimals are not supported

Looks like that there is no complete support for all number types.

@Jand42

This comment has been minimized.

Member

Jand42 commented Jun 18, 2018

Hi!

These errors are from missing proxies (JS implementations) for F# helper functions in the Microsoft.FSharp.Core.Operators module. They indeed could be added. For quick a workaround, you can add one yourselves:

[<Proxy "Microsoft.FSharp.Core.Operators, FSharp.Core">]
module internal OperatorsProxy =
   
   [<Macro(typeof<WebSharper.Core.Macros.Conversion>)>]
   let ToByte (x: 'T) = X<byte>

Actually there is support for all standard numeric types with some caveats.

  • Decimal, Complex and BigInteger need the WebSharper.MathJS package installed (as they will use math.js for correct precision and arithmetic). Decimal has full conversion support, the other two is lacking yet.
  • Integral types are enforced to have integral values on conversions and operations. For example int 4.3 becomes 4 as expected and 10 / 3 is 3.
  • There are no overflow checks on either conversions or operations.

Proposal: adding overflow checks to every small type on conversions but not on arithmetic could be a good tradeoff, not littering translated code unnecessary with checks but giving the expected results when using conversions to smaller types explicitly.

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