Skip to content

match []u8 gives &[]u8 #24054

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

Open
einar-hjortdal opened this issue Mar 26, 2025 · 6 comments
Open

match []u8 gives &[]u8 #24054

einar-hjortdal opened this issue Mar 26, 2025 · 6 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@einar-hjortdal
Copy link
Contributor

einar-hjortdal commented Mar 26, 2025

Describe the bug

In the code here the compiler gives me the following error:

src/protocol.v:472:33: error: cannot use `&[]u8` as `[]u8` in argument 1 to `bytes_to_blr`
  470 |             []u8 {
  471 |                 if param.len < max_char_length {
  472 |                     blr, value := bytes_to_blr(param)
      |                                                ~~~~~
  473 |                     _ := b.write(blr) or { 0 } // does not return any error
  474 |                     _ := v.write(value) or { 0 } // does not return any error

But the type matched []u8, why am I getting &[]u8?

Reproduction Steps

attempt to compile the linked repo

Expected Behavior

no error

Current Behavior

error

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 e491eb0

Environment details (OS name and version, etc.)

|V full version      |V 0.4.10 537605a.e491eb0
|:-------------------|:-------------------
|OS                  |linux, "openmamba release 2024.6 for x86_64 (rolling)"
|Processor           |8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
|Memory              |11.83GB/31.16GB
|                    |
|V executable        |/home/einar/.local/lib64/v/v
|V last modified time|2025-03-26 15:49:42
|                    |
|V home dir          |OK, value: /home/einar/.local/lib64/v
|VMODULES            |OK, value: /home/einar/.vmodules
|VTMP                |OK, value: /tmp/v_1000
|Current working dir |OK, value: /home/einar/Documents/projects/vlang/active/firebird
|                    |
|Git version         |git version 2.49.0
|V git status        |weekly.2025.13-16-ge491eb05
|.git/config present |true
|                    |
|cc version          |cc (GCC) 14.2.1 20240909
|gcc version         |gcc (GCC) 14.2.1 20240909
|clang version       |N/A
|tcc version         |tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
|tcc git status      |thirdparty-linux-amd64 0134e9b9
|emcc version        |N/A
|glibc version       |ldd (GNU libc) 2.41

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.

@einar-hjortdal einar-hjortdal added the Bug This tag is applied to issues which reports bugs. label Mar 26, 2025
Copy link

Connected to Huly®: V_0.6-22445

@jorgeluismireles
Copy link

I tested this oversimplification of your code in the playground compiles ok:

type Value = string | []u8

fn bytes_to_blr(bytes []u8) {
	println('${bytes}')
}

fn params_to_blr(params []Value) {
	for i := 0; i < params.len; i++ {
		param := params[i]
		match param {
			string {
				bytes_to_blr(param.bytes())			
			}
			[]u8 {
				bytes_to_blr(param)
			}
		}
	}
}

I didn't check your Value sum type details nor your bytes_to_blr arguments though.

@felipensp
Copy link
Member

I tested this oversimplification of your code in the playground compiles ok:

type Value = string | []u8

fn bytes_to_blr(bytes []u8) {
println('${bytes}')
}

fn params_to_blr(params []Value) {
for i := 0; i < params.len; i++ {
param := params[i]
match param {
string {
bytes_to_blr(param.bytes())
}
[]u8 {
bytes_to_blr(param)
}
}
}
}
I didn't check your Value sum type details nor your bytes_to_blr arguments though.

This is because Value is not a sumtype, but an empty interface. interface Value {}

@felipensp
Copy link
Member

Since it is an interface one, use bytes_to_blr(*param) @einar-hjortdal

@einar-hjortdal
Copy link
Contributor Author

einar-hjortdal commented Mar 27, 2025

Is there a reason why this happens, and why does the compiler error with []u8 and not with other types?

@einar-hjortdal
Copy link
Contributor Author

I tested this oversimplification of your code in the playground compiles ok:

You are correct, this does not happen with sum types.

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

No branches or pull requests

3 participants