Fix 'type inference too complicated' by REMOVING type annotations
- Dominant language
- F#
- Stars
- 4.3k
- Forks
- 876
- Avg merge
- 4d 11h
- Merged PRs (30d)
- 131
Description
**Succinct description of the issue**
I encountered error FS0465
> Type inference problem too complicated (maximum iteration depth reached). Consider adding further type annotations.
but the only fix I found was to _remove_ type annotations.
**Repro steps**
Consider this code.
```F#
type Vector<'a> =
{ vX: 'a
vY: 'a } with
// fails to compile with the next line
static member inline (*) ((s: 's), (v: Vector<'s>)) : Vector<'s> = { vX = s * v.vX; vY = s * v.vY }
// successfully compiles with the next line
//static member inline (*) (s, v) = { vX = s * v.vX; vY = s * v.vY }
type Matrix<'a> =
{ m11: 'a; m12: 'a; m13: 'a
m21: 'a; m22: 'a; m23: 'a
m31: 'a; m32: 'a; m33: 'a } with
static member inline (/) ((m: Matrix<'b>), (s: 'b)) : Matrix<'b> =
{ m11 = m.m11 / s; m12 = m.m12 / s; m13 = m.m13 / s
m21 = m.m21 / s; m22 = m.m22 / s; m23 = m.m23 / s
m31 = m.m31 / s; m32 = m.m32 / s; m33 = m.m33 / s }
static member inline (*) ((v: Vector<'b>), (m: Matrix<'b>)) : Vector<'b> =
{ vX = v.vX * m.m11 + v.vY * m.m21
vY = v.vX * m.m12 + v.vY * m.m22 }
module Matrix =
let inline determinant (m: Matrix<'a>) : 'a =
m.m11 * m.m22 * m.m33
+ m.m12 * m.m23 * m.m31
+ m.m13 * m.m21 * m.m32
- m.m31 * m.m22 * m.m13
- m.m32 * m.m23 * m.m11
- m.m33 * m.m21 * m.m12
let inline inverse m =
{ m11 = m.m22 * m.m33 - m.m32 * m.m23; m12 = m.m13 * m.m32 - m.m33 * m.m12; m13 = m.m12 * m.m23 - m.m22 * m.m13
m21 = m.m23 * m.m31 - m.m33 * m.m21; m22 = m.m11 * m.m33 - m.m31 * m.m13; m23 = m.m13 * m.m21 - m.m23 * m.m11
m31 = m.m21 * m.m32 - m.m31 * m.m22; m32 = m.m12 * m.m31 - m.m32 * m.m11; m33 = m.m11 * m.m22 - m.m21 * m.m12 }
/ (determinant m)
let inline mapVectorViaInverse matrix v = v * (matrix |> Matrix.inverse)
let f (m: Matrix) (v: Vector) =
let mapVectorViaInverse2 = m |> mapVectorViaInverse
v |> mapVectorViaInverse2 |> ignore
()
```
[ZIP](https://github.com/dotnet/fsharp/files/4724104/MWE.zip) of solution containing that code.
**Expected behavior**
The code compiles.
**Actual behavior**
The codes doesn't compile. Here is the build output.
```
1>------ Build started: Project: Temp, Configuration: Debug Any CPU ------
1>C:\Code\Temp\Program.fs(40,43): error FS0465: Type inference problem too complicated (maximum iteration depth reached). Consider adding further type annotations.
1>Done building project "Temp.fsproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
```
**Known workarounds**
Contrary to the suggestion in the build error, the only fix I found was to _remove_ type annotations.
Specifically, comment in the commented out line and then comment out the line it replaces.
**Related information**
* Operating system: WIndows 10 Enterprise Version 10.0.18362 Build 18362
* .NET Runtime kind: The MWE targets .NET Core and my most recent version of that is 3.1.202 (x64)
* Editing Tools: Visual Studio Enterprise 2019 Version 16.5.5
Contributor guide
Assessment
This issue has not been assessed yet.