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

Unknown method or field when using generics #22418

Closed
CG-SS opened this issue Oct 6, 2024 · 2 comments · Fixed by #22447
Closed

Unknown method or field when using generics #22418

CG-SS opened this issue Oct 6, 2024 · 2 comments · Fixed by #22447
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics.

Comments

@CG-SS
Copy link

CG-SS commented Oct 6, 2024

Describe the bug

According to the specs, this should be possible:

module main

struct Seq[T] {
	ar []T
}

type MapFn[T, K] = fn (T) K
fn (s Seq[T]) map[K](map_fn MapFn[T, K]) Seq[K] {
	mut map_ar := []K{cap: s.ar.len}

	for _, i in s.ar {
		map_ar << map_fn[T, K](i)
	}

	return Seq[K]{map_ar}
}

fn main() {
	s := Seq[string]{["one", "two"]}
		.map[int](fn (element string) int {
			match element {
				"one" { return 1 }
				"two" {return 2}
				else {return -1}
			}
		})
		.map[int](fn (element int) int {
			return element + 2;
		})

	println(s)
}

Reproduction Steps

Try to compile the provided code snippet.

Expected Behavior

It should compile.

Current Behavior

I get the error:

error: unknown method or field: `Seq[K].map`
   55 |             }
   56 |         })
   57 |         .map[int](fn (element int) int {
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   58 |             return element + 2;
   59 |         })

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 18eee34.209c30f

Environment details (OS name and version, etc.)

V full version: V 0.4.8 18eee34.209c30f
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: 0.4.8-40-g209c30f3-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 CG-SS added the Bug This tag is applied to issues which reports bugs. label Oct 6, 2024
@felipensp felipensp added the Generics[T] Bugs/feature requests, that are related to the V generics. label Oct 6, 2024
@jorgeluismireles
Copy link

shouldn't element be always string === T?

fn main() {
	s := Seq[string]{['one', 'two']} // T is string
	s.map[int](fn (element string) int { // K is int
		match element {
			'one' { return 1 }
			'two' { return 2 }
			else { return -1 }
		}
	})
	s.map[f32](fn (element string) f32 { // K is f32
		match element {
			'one' { return 1.0 }
			'two' { return 2.0 }
			else { return -1.0 }
		}
	})
	println(s)

@jorgeluismireles
Copy link

Sorry, now I got it... still learning...

s := Seq[string]{["one", "two"]}
	t := s.map[int](fn (element string) int {
		match element {
			"one" { return 1 }
			"two" {return 2}
			else {return -1}
		}
	})
	println('s: ${s.map}') // s.map exists
	println('t: ${t}') // t.map doesn't exist

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

Successfully merging a pull request may close this issue.

3 participants