Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to obtain generic results using the following function? #21884

Closed
xiusin opened this issue Jul 17, 2024 · 2 comments
Closed

How to obtain generic results using the following function? #21884

xiusin opened this issue Jul 17, 2024 · 2 comments
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics.

Comments

@xiusin
Copy link
Contributor

xiusin commented Jul 17, 2024

Describe the bug

I want to obtain the mapping relationship between ID and object through a function, but I am currently unable to achieve the expected result

Reproduction Steps

pub fn records_to_map[K, T](records []T, field_ string) map[K]T {
	mut result := map[K]T{}

	$for field in T.fields {
		for _, record in records {
			if field.name == field_ {
				result[record.$(field.name)] = record
			}
		}
	}
	return result
}



// test 

struct TestStruct {
pub mut:
	id i64
	name string
}

fn test_records_to_map () {
	mut records := []TestStruct{}
	records << TestStruct{
		id: 1
		name: 'aa'
	}
	records << TestStruct{
		id: 2
		name: 'bb'
	}

	records_to_map[i64, TestStruct](records, "id")
}

Expected Behavior

map[i64]TestStruct{1: {}, 2: {}}

Current Behavior

src/core/internal/fn.v:10:11: error: invalid key: expected `i64`, not `string`
    8 |         for _, record in records {
    9 |             if field.name == field_ {
   10 |                 result[record.$(field.name)] = record
      |                       ~~~~~~~~~~~~~~~~~~~~~~
   11 |             }
   12 |         }
checker summary: 1 V errors, 0 V warnings, 0 V notices

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.6 304aa9f

Environment details (OS name and version, etc.)

V full version: V 0.4.6 a1bb84f.304aa9f
OS: macos, macOS, 13.5, 22G74
Processor: 8 cpus, 64bit, little endian, Apple M2

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.

@xiusin xiusin added the Bug This tag is applied to issues which reports bugs. label Jul 17, 2024
@felipensp
Copy link
Member

In this case you need to check for the field type to use it as index key.

pub fn records_to_map[K, T](records []T, field_ string) map[K]T {
	mut result := map[K]T{}

	$for field in T.fields {
		for _, record in records {
			if field.name == field_ {
				$if field.typ is K {
					result[record.$(field.name)] = record
				}
			}
		}
	}
	return result
}

So

dump(records_to_map[int, TestStruct](records, "id"))
dump(records_to_map[string, TestStruct](records, "name"))

@felipensp felipensp added the Generics[T] Bugs/feature requests, that are related to the V generics. label Jul 18, 2024
@xiusin
Copy link
Contributor Author

xiusin commented Jul 18, 2024

@felipensp Okay, thank you. Do you need me to close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics.
Projects
None yet
Development

No branches or pull requests

2 participants