v icon indicating copy to clipboard operation
v copied to clipboard

Fails to deduce generic type K from internal generic chain

Open CG-SS opened this issue 4 months ago • 0 comments

Describe the bug

It seems that the builder fails to deduce the correct type when chaining generic types.

Reproduction Steps

Try to run the following program:

module main

struct In[T] {
	internal chan T
}

fn init[T](i T) In[T] {
	return In[T]{i}
}

type MapFn[T, K] = fn (T) K

fn do_map[T, K](from chan T, to chan K, map_fn MapFn[T, K]) {
	to <- map_fn[T, K](<-from)
}

fn (i In[T]) map[K](map_fn MapFn[T, K]) In[K] {
	k_src := In[K]{}

	do_map[T, K](i.internal, k_src.internal, map_fn)

	return k_src
}

fn (i In[T]) get() T {
	return <-i.internal
}

fn main() {
	result := init[int](1).map[string](fn (i int) string {
		match i {
			1 { return "one" }
			else { return "zero" }
		}
	}).get()

	println(result)
}

Expected Behavior

Prints one.

Current Behavior

main.v:20:27: error: cannot use `chan int` as `chan string` in argument 2 to `do_map`
   18 |     k_src := In[K]{}
   19 | 
   20 |     do_map[T, K](i.internal, k_src.internal, map_fn)
      |                              ~~~~~~~~~~~~~~
   21 | 
   22 |     return k_src

Since we provide the type as string, the builder should be able to realize that K is of type chan string not chan int, which is T.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 18eee34.0cf3a44

Environment details (OS name and version, etc.)

V full version: V 0.4.8 18eee34.0cf3a44
OS: windows, Microsoft Windows 11 Pro v22631 64-bit
Processor: 32 cpus, 64bit, little endian

Git version: git version 2.42.0.windows.2
Git vroot status: weekly.2024.41-15-g0cf3a445-dirty
.git/config present: true

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

CG-SS avatar Oct 11 '24 22:10 CG-SS