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

x.json2,checker,toml: allow field.typ compile-time checking with MatchExpr and add array of option checking #21171

Merged
merged 4 commits into from
Apr 4, 2024

Conversation

felipensp
Copy link
Member

@felipensp felipensp commented Apr 2, 2024

Fix #21170
Improve type checking from #21163

@felipensp felipensp changed the title checker: allow field.typ compile-time checking with MatchExpr and add array of option checking x.json2,checker: allow field.typ compile-time checking with MatchExpr and add array of option checking Apr 2, 2024
@ttytm
Copy link
Member

ttytm commented Apr 2, 2024

Awesome! The same can be applied to toml then!

vlib/x/json2/decoder.v Outdated Show resolved Hide resolved
@MCausc78
Copy link
Contributor

MCausc78 commented Apr 2, 2024

what about maps?

vlib/x/json2/decoder.v Outdated Show resolved Hide resolved
@felipensp
Copy link
Member Author

what about maps?

It does works.

struct Test {
	a int
	b []int
	c map[int]string
}

fn main() {
	$for f in Test.fields {
		type_name := typeof(f.$(f.name)).name
		match f.typ {
			int { println('1. ${f.name} is ${type_name}') }
			[]int { println('2. ${f.name} is ${type_name}') }
			map[int]string { println('3. ${f.name} is ${type_name}') }
			else {}
		}
	}
}

@felipensp felipensp changed the title x.json2,checker: allow field.typ compile-time checking with MatchExpr and add array of option checking x.json2,checker,toml: allow field.typ compile-time checking with MatchExpr and add array of option checking Apr 2, 2024
@Delta456
Copy link
Member

Delta456 commented Apr 3, 2024

what about maps?

It does works.

struct Test {
	a int
	b []int
	c map[int]string
}

fn main() {
	$for f in Test.fields {
		type_name := typeof(f.$(f.name)).name
		match f.typ {
			int { println('1. ${f.name} is ${type_name}') }
			[]int { println('2. ${f.name} is ${type_name}') }
			map[int]string { println('3. ${f.name} is ${type_name}') }
			else {}
		}
	}
}

Is there a way to check the key and the value type of a map separately?

@felipensp
Copy link
Member Author

felipensp commented Apr 3, 2024

what about maps?

It does works.

struct Test {
	a int
	b []int
	c map[int]string
}

fn main() {
	$for f in Test.fields {
		type_name := typeof(f.$(f.name)).name
		match f.typ {
			int { println('1. ${f.name} is ${type_name}') }
			[]int { println('2. ${f.name} is ${type_name}') }
			map[int]string { println('3. ${f.name} is ${type_name}') }
			else {}
		}
	}
}

Is there a way to check the key and the value type of a map separately?

No yet. This would be an another feature request.

@felipensp felipensp marked this pull request as ready for review April 4, 2024 00:21
@spytheman spytheman merged commit 625048c into vlang:master Apr 4, 2024
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

have a better to way to detect arrays from compile time
6 participants