v icon indicating copy to clipboard operation
v copied to clipboard

Anonymous generic function inside of a generic function

Open Dracks opened this issue 1 year ago • 5 comments

Describe the bug

Code: https://vosca.dev/p/fb9c42f729

When you have a generic function that accepts two types, and has an anonymous function inside, If you put only one type (and not the second, as this case that is used both), it give you a weird error.

I hope the people that knew better V-lang, can explain better the problem

struct Test{
	something string
}

struct Dependency {}

struct Factory {
	build fn()!
}
fn  inject[T](mut serv T)!{
	$for field in T.fields {
		if field.typ is string {
			serv.$(field.name) = "Hello world!"
		}
	}
}

fn use_factory[F, E](factory fn(dep F) !E) Factory{
	return Factory{
		build: fn [factory][F]()!{
				mut dep := F{}
				inject[F](mut dep)!
				dump(factory(dep)!)
		}
	}
}

use_factory[Dependency, Test](fn (dep Dependency)!Test {
	return Test{
		something: 'daleks!'
	}
}).build()!

Expected Behavior

Complain that the anonymous function misses the second generic parameter

Current Behavior

Output:

code.v:11:16: error: unknown type `F`
    9 | }
   10 | fn  inject[T](mut serv T)!{
   11 |     $for field in T.fields {
      |                   ^
   12 |         if field.typ is string {
   13 |             serv.$(field.name) = "Hello world!"
If the code of your project is in multiple files, try with `v .` instead of `v code.v`
Exited with error status 1

Reproduction Steps

For what I found you need a function, that creates an anonymous function that handles both types

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.0 8ee1667

Environment details (OS name and version, etc.)

V full version: V 0.4.0 fc4c431.8ee1667
OS: linux, Ubuntu 22.04.2 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz

getwd: /home/pmakhnev/playground
vexe: /home/pmakhnev/v/v
vexe mtime: 2023-08-02 12:00:10

vroot: OK, value: /home/pmakhnev/v
VMODULES: OK, value: /root/.vmodules
VTMP: OK, value: /tmp/v_0

Git version: git version 2.34.1
Git vroot status: weekly.2023.30-63-g8ee1667a
.git/config present: true

CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

Dracks avatar Aug 02 '23 15:08 Dracks