v icon indicating copy to clipboard operation
v copied to clipboard

v: comptime type lowercase ($int, $enum, $option, etc)

Open felipensp opened this issue 1 year ago • 2 comments

This PR turns compile-time type ($int, $enum, $map, etc) lowercased.

interface Abc {}

enum EnumTest {
	a
	b
}

type Alias = Abc

type Sumtype = int | string

struct Foo {}

struct Test {
	a Abc
	b map[string]string
	c []int
	d EnumTest
	e int
	f f64
	g Alias
	h Foo
	i fn ()
	j Sumtype
	k ?int
}

fn test_comptime_types() {
	mut i := 0
	mut a := 0
	mut m := 0
	mut e := 0
	mut c_int := 0
	mut c_float := 0
	mut c_alias := 0
	mut c_struct := 0
	mut c_fn := 0
	mut c_sum := 0
	mut c_option := 0
	$for f in Test.fields {
		$if f.typ is $alias {
			c_alias++
		} $else $if f.typ is $interface {
			i++
		} $else $if f.typ is $array {
			a++
		} $else $if f.typ is $map {
			m++
		} $else $if f.typ is $enum {
			e++
		} $else $if f.typ is $int {
			c_int++
		} $else $if f.typ is $float {
			c_float++
		} $else $if f.typ is $struct {
			c_struct++
		} $else $if f.typ is $function {
			c_fn++
		} $else $if f.typ is $sumtype {
			c_sum++
		} $else $if f.typ is $option {
			c_option++
		}
	}
	assert i == 1
	assert a == 1
	assert m == 1
	assert e == 1
	assert c_int == 1
	assert c_float == 1
	assert c_alias == 1
	assert c_struct == 1
	assert c_fn == 1
	assert c_sum == 1
	assert c_option == 1
}

felipensp avatar Mar 22 '23 01:03 felipensp

Good work, however, it would be breaking to existing code:

o_test.v:13:18: error: only `$tmpl()`, `$env()`, `$embed_file()`, `$pkgconfig()`, `$vweb.html()`, `$compile_error()` and `$compile_warn()` comptime functions are supported right now
   11 | [inline]
   12 | pub fn func2[T](t &T) {
   13 |     $if T is $Array {

Also v fmt -w file.v can not be used to upgrade that existing code, since the error above is a parser one.

spytheman avatar Mar 22 '23 16:03 spytheman

@medvednikov do we need to support migration of the older code written with $Array to $array ?

spytheman avatar Mar 22 '23 16:03 spytheman

@spytheman I think it's fine. It's a relatively new feature, and it's comptime, so a pretty specific use case.

medvednikov avatar Mar 22 '23 23:03 medvednikov

Thanks @felipensp

Feels a lot more natural.

medvednikov avatar Mar 22 '23 23:03 medvednikov