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

builder error: using generics #20065

Closed
enghitalo opened this issue Dec 2, 2023 · 0 comments · Fixed by #20070
Closed

builder error: using generics #20065

enghitalo opened this issue Dec 2, 2023 · 0 comments · Fixed by #20070
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@enghitalo
Copy link
Contributor

enghitalo commented Dec 2, 2023

Describe the bug

encode(val.$method()) // Problem here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!!!11!

Reproduction Steps

struct Human {
	name string
}

enum Animal {
	dog
	cat
}

type Entity = Animal | Human

@[sumtype_to: Animal]
fn (ent Entity) json_cast_to_animal() Animal {
	return ent as Animal
}

@[sumtype_to: Human]
fn (ent Entity) json_cast_to_human() Human {
	return ent as Human
}

fn encode[T](val T) {
	$if T is $sumtype {
		$for method in T.methods {
			if method.attrs.len >= 1 {
				if method.attrs[0].contains('sumtype_to') {
					if val.type_name() == method.attrs[0].all_after('sumtype_to:').trim_space() {
						println(val.$method())
						encode(val.$method()) // Problem here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!!!11!
					}
				}
			}
		}
	} $else {
		// error('cannot encode value with ${typeof(val).name} type')
	}
}

encode(Entity(Human{'Monke'}))
encode(Entity(Animal.cat))

Expected Behavior

Human{
    name: 'Monke'
}
cat

Current Behavior

==================
/tmp/v_1000/copy.8521023635428838776.tmp.c:15353: error: cannot convert 'enum <anonymous>' to 'struct string'
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.3 be51143

Environment details (OS name and version, etc.)

Linux Ubuntu

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.

@enghitalo enghitalo added the Bug This tag is applied to issues which reports bugs. label Dec 2, 2023
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant