You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: type inference failing for nested type parameters #39661
If a function is called that takes an argument of a generic type that in turn has its own type parameter, the inference of the subtype fails. For example,
I concur with @ianlancetaylor: I believe the current inference algorithm is not designed to handle this case. Though it seems that perhaps it could (should?). Keeping open for future investigation.
@rogpeppe Took the liberty to include the current solution for your problem https://go2goplay.golang.org/p/44c92Nqchqx For me its very important work arounds are always included because people like me who aren't that smart spend hours sometimes understanding why it doesn't work. Including a work around helps allot understanding the problem.
package main
import (
"fmt"
)
typeF(typeT) interface {
X() T
}
funccallx(typeT)(fF(T)) T {
returnf.X()
}
typefoostruct{}
func (foo) X() int {
return99
}
funcmain() {
// fmt.Println(callx(foo{})) // this should be made to work alsofmt.Println(callx(int)(foo{}))
}
I apologize if my comment is unrelated, but based on the example and the error code I wanted to see whether it was related, or if I'm doing something wrong with my code. I couldn't find an explicit reason in the design doc that would prohibit this case from working.
Goal: I am trying to write a generic version of a quicksort algorithm that uses generic constraints. The example given is the design doc introduces a typed interface called Ordered which is useful for built in types, but is inaccessible for user defined structs. Therefore I have introduced a QuickSortable interface which exposes an Ordered value.
// Ordered is a type constraint that matches all ordered types.// (An ordered type is one that supports the < <= >= > operators.)// In practice this type constraint would likely be defined in// a standard library package.// Taken from the proposaltypeOrderedinterface {
typeint, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, string
}
/* package quicksort */// QuickSortable is the generic interface required to use the QuickSort// function. This interface allows arbitrary user defined types// to take advantage of the generic sorting algorithm.typeQuickSortable(typeTOrdered) interface {
Orderable() T
}
I have three function signatures that relate the types in the following way:
// partition runs a single sorting iteration. Given a slice to sort,// partition begins at the low index, and while iterating to the high index// swapping out of place elements on the way.funcpartition(typeOOrdered, QQuickSortable(O))(q []Q, lowIdxint, highIdxint) (int, error) {...}
// quickSort is the recursive component of the quicksort implementation.// It delegates down to partition to run a single iteration// and exhausts itself when the low index is greater than the high index.funcquickSort(typeOOrdered, QQuickSortable(O))(q []Q, lowIdxint, highIdxint) error {...}
// QuickSort is the generic outer shell, which delegates to the// private quickSort function, that is exported as the public API.// type O Ordered is used so that the operator <= is legal.// type Q is used to produce an instance of type O via the call to Orderable(),// which allows any user defined type to take advantage of QuickSort.// The type signature is propagated down to the internal functions.funcQuickSort(typeOOrdered, QQuickSortable(O))(q []Q) error {...}
The user struct implements the QuickSortable interface in the following ways:
// QSInt is a type alias of int, which allows a slice of QSInt// to utilize quicksort.typeQSIntint// Orderable implements the QuickSortable interface so that QSInt// can be used in the QuickSort function.func (qQSInt) Orderable() int {
returnint(q)
}
// Guest is a domain object of some application that needs sortingtypeGueststruct {
namestringageint
}
// Orderable implements the QuickSortable interface so that Guest// can be used in the quicksort function.func (gGuest) Orderable() int {
returnint(g.age)
}
Later I try to invoke the QuickSort function in the following ways:
varsortList []QuickSortable(int)
// other codeerr:=QuickSort(int, QuickSortable(int))(sortList)
varguestList []QuickSortable(int)
// other codeerr=QuickSort(int, QuickSortable(int))(guestList)
Output: The type checking fails because O (Orderable) cannot be inferred.
type checking failed for main
prog.go2:63:52: cannot infer O (prog.go2:40:21)
prog.go2:68:38: cannot infer O (prog.go2:61:21)
prog.go2:69:39: cannot infer O (prog.go2:61:21)
prog.go2:81:33: cannot infer O (prog.go2:61:21)
I have made a few attempts to get this working, and the only method that would run the code as expected required dropping the type O Orderable, and instead injecting int directly into the QuickSortable type parameter: https://go2goplay.golang.org/p/KElHD5oZWgf
changed the title [-]cmd/go2go: type inference failing for nested type parameters[/-][+]cmd/compile: type inference failing for nested type parameters[/+]on Mar 8, 2023
works now on the 1.21 dev branch (playground). It was (most likely) addressed with CL 472298.
It is different from @rogpeppe 's example (updated and slightly simplified). That example is essentially covered with issue #41176 so I am going to close this specific issue as fixed.
@mcgraw-bb25 Your long example also seems to work fine when adjusted to new syntax (playground link). Again, this requires 1.21 dev branch. It doesn't work with 1.20.
Activity
DeedleFake commentedon Jun 17, 2020
@gopherbot, assign @griesemer.
ianlancetaylor commentedon Jun 17, 2020
I don't think the algorithm in the design draft explains how to handle this case. If we fix this, we should update the description of the algorithm.
gertcuykens commentedon Jun 17, 2020
@DeedleFake Doesn't work for me? Can you paste the working example please.
DeedleFake commentedon Jun 17, 2020
@gertcuykens:
Playground
griesemer commentedon Jun 18, 2020
I concur with @ianlancetaylor: I believe the current inference algorithm is not designed to handle this case. Though it seems that perhaps it could (should?). Keeping open for future investigation.
rogpeppe commentedon Jun 18, 2020
Here's a slightly simpler example. I think this should be made to work: https://go2goplay.golang.org/p/3-aVhD6Y9R2
gertcuykens commentedon Jun 18, 2020
@rogpeppe Took the liberty to include the current solution for your problem https://go2goplay.golang.org/p/44c92Nqchqx For me its very important work arounds are always included because people like me who aren't that smart spend hours sometimes understanding why it doesn't work. Including a work around helps allot understanding the problem.
bcmills commentedon Jun 23, 2020
This crops up in a number of other places too, such as
appendand analogous libraries for transforming and/or aggregating slices and maps.mcgraw-bb25 commentedon Jul 2, 2020
I apologize if my comment is unrelated, but based on the example and the error code I wanted to see whether it was related, or if I'm doing something wrong with my code. I couldn't find an explicit reason in the design doc that would prohibit this case from working.
Go Version: https://go2goplay.golang.org/
Reproduce via: https://go2goplay.golang.org/p/UDaKcuC6pVd
Goal: I am trying to write a generic version of a quicksort algorithm that uses generic constraints. The example given is the design doc introduces a typed interface called
Orderedwhich is useful for built in types, but is inaccessible for user defined structs. Therefore I have introduced aQuickSortableinterface which exposes anOrderedvalue.I have three function signatures that relate the types in the following way:
The user struct implements the QuickSortable interface in the following ways:
Later I try to invoke the QuickSort function in the following ways:
Output: The type checking fails because O (Orderable) cannot be inferred.
I have made a few attempts to get this working, and the only method that would run the code as expected required dropping the type O Orderable, and instead injecting
intdirectly into the QuickSortable type parameter: https://go2goplay.golang.org/p/KElHD5oZWgfCan you please confirm is this is an actual issue or I'm misinterpreting the design doc and this shouldn't be possible?
Thank you very much.
[-]cmd/go2go: type inference failing for nested type parameters[/-][+]cmd/compile: type inference failing for nested type parameters[/+]griesemer commentedon Mar 8, 2023
The original example in this issue, updated to new syntax:
works now on the 1.21 dev branch (playground). It was (most likely) addressed with CL 472298.
It is different from @rogpeppe 's example (updated and slightly simplified). That example is essentially covered with issue #41176 so I am going to close this specific issue as fixed.
griesemer commentedon Mar 8, 2023
@mcgraw-bb25 Your long example also seems to work fine when adjusted to new syntax (playground link). Again, this requires 1.21 dev branch. It doesn't work with 1.20.
gopherbot commentedon May 31, 2023
Change https://go.dev/cl/499282 mentions this issue:
doc/go1.21: document type inference changesdoc/go1.21: document type inference changes