Skip to content
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

Support custom Math operators #3682

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### All

* [GH-3668](https://github.com/fable-compiler/Fable/pull/3668) Normalize fable-library argument (by @nojaf)
* [GH-3682](https://github.com/fable-compiler/Fable/pull/3682)Support some custom unary math operors (Acos, Asin, Atan, Atan2, Cos, Cosh, Exp, Log, Log2, Log10, Sin, Sinh, Sqrt, Tan, Tanh) (by @PierreYvesR)

#### Python

Expand Down
6 changes: 5 additions & 1 deletion src/Fable.Transforms/Dart/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,11 @@ let operators
| "Sinh", _
| "Sqrt", _
| "Tan", _
| "Tanh", _ -> math r t args i.SignatureArgTypes [] i.CompiledName |> Some
| "Tanh", _ ->
match args with
| ExprType(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
6 changes: 5 additions & 1 deletion src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,11 @@ let operators
| "Sin", _
| "Sinh", _
| "Tan", _
| "Tanh", _ -> math r t args i.SignatureArgTypes i.CompiledName |> Some
| "Tanh", _ ->
match args with
| ExprType(Number(_, _)) :: _ ->
math r t args i.SignatureArgTypes i.CompiledName |> Some
| _ -> applyOp com ctx r t i.CompiledName args |> Some
| "Log", _
| "Sqrt", _ ->
Helper.LibCall(
Expand Down
6 changes: 5 additions & 1 deletion src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2143,7 +2143,11 @@ 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
_ ->
match args with
| ExprType(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
6 changes: 5 additions & 1 deletion src/Fable.Transforms/Rust/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,11 @@ 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
_ ->
match args with
| ExprType(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
Loading