v icon indicating copy to clipboard operation
v copied to clipboard

feat: $if (int(typeof(val).idx) >> 16) & 0xff == 0 {

Open enghitalo opened this issue 1 year ago • 11 comments

Describe the feature

Every info there is knowed at compile time there are not why it not be allowed

Use Case

json2 encode/decode

struct Encoder {}

fn main() {
	mut e := &Encoder{}

	val := ''

	e.encode_value(&val)
}

pub fn (e &Encoder) encode_value[T](val T) {
	mut nr_muls := 0
	$if (int(typeof(val).idx) >> 16) & 0xff == 0 {
	} $else {
		nr_muls = (int(typeof(val).idx) >> 16) & 0xff
	}
}

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • [ ] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

Version used

V 0.4.3 bf7b29a

Environment details (OS name and version, etc.)

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 avatar Jan 05 '24 12:01 enghitalo

Does this not work?

Delta456 avatar Jan 05 '24 12:01 Delta456

Does this not work?

This give me a check error

vlib/x/json2/encoder.v:41:42: error: invalid `$if` condition: v.ast.InfixExpr1
   39 | // encode_value encodes a value to the specific buffer.
   40 | pub fn (e &Encoder) encode_value[T](val T, mut buf []u8) ! {
   41 |     $if (int(typeof(val).idx) >> 16) & 0xff == 0 {
      |                                             ~~
   42 | 
   43 |     }

enghitalo avatar Jan 05 '24 12:01 enghitalo

It compiles correctly for me. image

Delta456 avatar Jan 05 '24 12:01 Delta456

It compiles correctly for me. image

I am confused now my v doctor send me yours

V full version: V 0.4.3 e2334d8.5a32898
OS: linux, Ubuntu 23.04
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 7 5800H with Radeon Graphics

getwd: /home/hitalo/Documents/v
vexe: /home/hitalo/Documents/v/v
vexe mtime: 2024-01-05 13:35:57

vroot: OK, value: /home/hitalo/Documents/v
VMODULES: OK, value: /home/hitalo/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.39.2
Git vroot status: weekly.2024.01.test2-9-g5a32898d-dirty
.git/config present: true

CC version: cc (Ubuntu 12.3.0-1ubuntu1~23.04) 12.3.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

enghitalo avatar Jan 05 '24 13:01 enghitalo

You ain't on the latest commit

V full version: V 0.4.3 e2334d8.c4541cb
OS: linux, Pop!_OS 22.04 LTS
Processor: 12 cpus, 64bit, little endian, AMD Ryzen 5 5600H with Radeon Graphics

getwd: /home/delta
vexe: /home/delta/Documents/git/v_dev/v
vexe mtime: 2024-01-05 12:53:03

vroot: OK, value: /home/delta/Documents/git/v_dev
VMODULES: OK, value: /home/delta/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.34.1
Git vroot status: weekly.2023.52-83-gc4541cb9-dirty
.git/config present: true

CC version: cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

Delta456 avatar Jan 05 '24 13:01 Delta456

@Delta456 https://play.vlang.io/p/4095644ccf What I am doing wrongly?

enghitalo avatar Jan 05 '24 13:01 enghitalo

The playground is fine for testing small things, but you need to v up and test locally.

If it works locally, but fails in the playground, it's a playground problem, not a V problem.

JalonSolov avatar Jan 05 '24 13:01 JalonSolov

Wait a minute. I'll be the judge. Haha

shove70 avatar Jan 05 '24 13:01 shove70

a.v:12:6: warning: unused variable: `nr_muls`
   10 | 
   11 | pub fn (e &Encoder) encode_value[T](val T) {
   12 |     mut nr_muls := 0
      |         ~~~~~~~
   13 |     $if (int(typeof(val).idx) >> 16) & 0xff == 0 {
   14 |     } $else {
a.v:13:42: error: invalid `$if` condition: v.ast.InfixExpr1
   11 | pub fn (e &Encoder) encode_value[T](val T) {
   12 |     mut nr_muls := 0
   13 |     $if (int(typeof(val).idx) >> 16) & 0xff == 0 {
      |                                             ~~
   14 |     } $else {
   15 |         nr_muls = (int(typeof(val).idx) >> 16) & 0xff

commit e2334d8b62ab49b338e1f1f58973fbd3e575eebf MacOS

shove70 avatar Jan 05 '24 13:01 shove70

@enghitalo maybe this can be closed?

esquerbatua avatar Jan 09 '24 10:01 esquerbatua

@enghitalo maybe this can be closed?

No

enghitalo avatar Jan 09 '24 10:01 enghitalo