fo icon indicating copy to clipboard operation
fo copied to clipboard

undefined: T in func x[T](p *T)(**T) when x[*T](x[T](a))

Open go-li opened this issue 7 years ago • 3 comments

Hello the following program fails to compile https://play.folang.org/p/2XxLi_ukWR5

package main

func x[T](p *T)(**T){
	return &p
}

func y[T](a *T){
	x[*T](x[T](a))
}

func main() {
}

go-li avatar Jul 31 '18 18:07 go-li

Closely related form:

package main

func dontrunthisfunction[T](p *T){
	dontrunthisfunction[*T](&p)
}

func main() {
}

go-li avatar Aug 01 '18 15:08 go-li

@go-li thank you for reporting this.

~~The issue lies with pointer dereference expressions of the form *T where T is a type parameter. After spending some time thinking about it, I believe these kinds of expressions should not be allowed and the compiler should report an error. (The current error message obviously doesn't make much sense so I would like to improve it by catching the issue earlier on in the type-checker).~~ Edit: I'm sorry, I was mistaken. The code you shared should not have anything that gets interpreted as a dereference expression. I'll investigate further and fix the issue as soon as I get a chance.

albrow avatar Aug 03 '18 21:08 albrow

Related to https://github.com/albrow/fo/issues/27.

Taking a closer look at your example:

func dontrunthisfunction[T](p *T){
	dontrunthisfunction[*T](&p)
}

Here, *T is a form of polymorphic recursion that will probably be disallowed by the type checker moving forward.

albrow avatar Nov 09 '18 22:11 albrow