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 type for json.decode with nested generics #22220

Open
sherbrow opened this issue Sep 14, 2024 · 1 comment
Open

Unknown type for json.decode with nested generics #22220

sherbrow opened this issue Sep 14, 2024 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules.

Comments

@sherbrow
Copy link

sherbrow commented Sep 14, 2024

Could be linked to #19045, but there are no anonymous functions here.
Probably some extension of #18308 as well.

V doctor:

V full version: V 0.4.7 6834ccb.423f804

CC version: Apple clang version 14.0.3 (clang-1403.0.22.14.1)
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

What did you do?

https://play.vlang.io/p/685a11aa5a

./v -g -o vdbg cmd/v && ./vdbg pg-generics.v

import json

struct Chocolate {
	with_nuts bool
}

struct Box[T] {
	items []T
}

fn open[T](text string) []T {
	// Doesn't compile (not without another explicit type declaration) :
	box := string_decode[Box[T]](text)

	// Compiles without the explicit type declaration :
	// box := json.decode(Box[T], text) or { panic(err) }
	return box.items
}

fn string_decode[L](text string) L {
	result := json.decode(L, text) or { panic(err) }

	return result
}

fn main() {
	// Explicitly declare type
	// _ := Box[Chocolate]{}

	box_text := '{"items": [
		{},
		{"with_nuts": true},
		{}
	]}'
	bonbons := open[Chocolate](box_text)

	println(bonbons)

	assert bonbons.len == 3
	assert bonbons[0].with_nuts == false
	assert bonbons[1].with_nuts == true
	assert bonbons[2].with_nuts == false
}

What did you expect to see?

No error, the type Box[Chocolate] should not be unknown

What did you see instead?

pg-generics.v:21:17: error: json.decode: unknown type `Box[Chocolate]`
   19 | 
   20 | fn string_decode[L](text string) L {
   21 |     result := json.decode(L, text) or { panic(err) }
      |                    ~~~~~~~~~~~~~~~
   22 | 
   23 |     return result

I also saw that declaring explicitly a variable with that type, or using directly json.decode would avoid the error.

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.

@esquerbatua esquerbatua added Bug This tag is applied to issues which reports bugs. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules. labels Sep 26, 2024
@esquerbatua
Copy link
Contributor

With x.json2 it's failing also:

import x.json2

struct Chocolate {
	with_nuts bool
}

struct Box[T] {
	items []T
}

fn open[T](text string) []T {
	// Doesn't compile (not without another explicit type declaration) :
	box := string_decode[Box[T]](text)

	// Compiles without the explicit type declaration :
	// box := json.decode(Box[T], text) or { panic(err) }
	return box.items
}

fn string_decode[L](text string) L {
	result := json2.decode[L](text) or { panic(err) }

	return result
}

fn main() {
	// Explicitly declare type
	// _ := Box[Chocolate]{}

	box_text := '{"items": [
		{},
		{"with_nuts": true},
		{}
	]}'
	bonbons := open[Chocolate](box_text)

	println(bonbons)

	assert bonbons.len == 3
	assert bonbons[0].with_nuts == false
	assert bonbons[1].with_nuts == true
	assert bonbons[2].with_nuts == false
}

Part of the C code:
typedef struct _result_main__Box[main__Chocolate] _result_main__Box[main__Chocolate];

@esquerbatua esquerbatua added Modules: x.json2 Bugs related to *only* x.json2 (the pure V implementation) and removed Modules: x.json2 Bugs related to *only* x.json2 (the pure V implementation) labels Sep 26, 2024
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. Modules: JSON Bugs/feature requests, that are related to `json` and `x.json2` modules.
Projects
None yet
Development

No branches or pull requests

2 participants