Skip to content

Commit

Permalink
Support custom Math operators
Browse files Browse the repository at this point in the history
single-operand only: log, exp and trigonometric functions
  • Loading branch information
PierreYvesR committed Dec 26, 2023
1 parent 67a5340 commit b27596c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2143,7 +2143,13 @@ let operators
|> Some
| _ -> math r t args i.SignatureArgTypes i.CompiledName |> Some
| ("Acos" | "Asin" | "Atan" | "Atan2" | "Cos" | "Cosh" | "Exp" | "Log" | "Log2" | "Log10" | "Sin" | "Sinh" | "Sqrt" | "Tan" | "Tanh"),
_ -> math r t args i.SignatureArgTypes i.CompiledName |> Some
_ ->
let argTypes = args |> List.map (fun a -> a.Type)

match argTypes with
| Number(_, _) :: _ ->
math r t args i.SignatureArgTypes i.CompiledName |> Some
| _ -> applyOp com ctx r t i.CompiledName args |> Some
| "Round", _ ->
match args with
| ExprType(Number(Decimal, _)) :: _ ->
Expand Down
5 changes: 5 additions & 0 deletions tests/Dart/src/CustomOperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MyRecord =
{ value: int }
static member (+) (x: MyRecord, y: int) = { value = x.value + y }
static member (+) (x: int, y: MyRecord) = x + y.value + 2
static member Exp (x: MyRecord) = { value = x.value + 1}

type CustomPow =
{ Ok: bool }
Expand All @@ -41,6 +42,10 @@ let typeOperators() =
let x = { Ok = false }
x ** 2 |> equal { Ok = true }

testCase "Custom Exp works" <| fun () ->
let x = { value = 0 }
x |> exp |> equal { value = 1 }

let (+) (x: int) (y: int) = x * y

let (-) (x: int) (y: int) = x / y
Expand Down
10 changes: 10 additions & 0 deletions tests/Js/Main/CustomOperatorsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type MyRecord =
static member (+) (x: MyRecord, y: int) = { value = x.value + y }
static member (+) (x: int, y: MyRecord) = x + y.value + 2

static member Exp (x: MyRecord) = { value = x.value + 1}

type CustomPow =
{ Ok: bool }
static member Pow(x: CustomPow, n: int) = { Ok = true }
Expand All @@ -40,6 +42,14 @@ let typeOperators = [
testCase "Custom Pow works" <| fun () -> // See #2496
let x = { Ok = false }
x ** 2 |> equal { Ok = true }

testCase "Custom Exp works" <| fun () ->
let x = { value=10 }
exp x |> equal { value= 11 }

testCase "Float Exp works" <| fun () ->
let x = 0.0
exp x |> equal 1.
]

let (+) (x: int) (y: int) = x * y
Expand Down
6 changes: 6 additions & 0 deletions tests/Python/TestCustomOperators.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MyRecord =
{ value: int }
static member (+) (x: MyRecord, y: int) = { value = x.value + y }
static member (+) (x: int, y: MyRecord) = x + y.value + 2
static member Exp (x: MyRecord) = { value = x.value + 1}

[<Fact>]
let ``test Custom operators with types work`` () =
Expand All @@ -35,6 +36,11 @@ let ``test Custom operator overloads work`` () =
x + 2 |> equal { value = 7 }
3 + x |> equal 10

[<Fact>]
let ``test Custom operator Exp works`` () =
let x = { value = 0 }
x |> exp |> equal { value = 1 }

let (+) (x: int) (y: int) = x * y

let (-) (x: int) (y: int) = x / y
Expand Down
7 changes: 7 additions & 0 deletions tests/Rust/tests/src/CustomOperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type MyStruct =
static member (<<<) (x: MyStruct, n: int) = { value = pown x.value n }
static member (~-) (x: MyStruct) = { value = -x.value }

static member Exp (x: MyStruct) = { value = exp x.value}

type MyRecord =
{ value: int }
static member (+) (x: MyRecord, y: int) = { value = x.value + y }
Expand Down Expand Up @@ -60,6 +62,11 @@ let ``Custom Pow works`` () = // See #2496
let x = { Ok = false }
x ** 2 |> equal { Ok = true }

[<Fact>]
let ``Custom Exp works`` () =
let x = { MyStruct.value = 0. }
x |> exp |> equal { MyStruct.value = 1.0 }

let (+) (x: int) (y: int) = x * y
let (-) (x: int) (y: int) = x / y
let (||||) x y = x + y
Expand Down

0 comments on commit b27596c

Please sign in to comment.