Skip to content

comptime need a way detect a field is shared or not #25600

Description

@kbkpbot

Describe the feature

When use generic/comptime , it need some way to detect a struct field is shared or not.

Use Case

a sample code, share.v :

import encoding.binary

struct MyS {
	a int
	cache shared map[u64]string
}

fn main() {
	mut s := MyS{}
	
	b := binary.encode_binary(s)!
	dump(b)
}
v share.v

compile this will just issue a error message :

$ v share.v
/media/HD/github/kbkpbot/v/vlib/encoding/binary/serialize.v:69:29: error: `value` is `shared` and must be `rlock`ed or `lock`ed to be passed as non-mut argument
   67 |                 encode_map(mut s, value)!
   68 |             } $else {
   69 |                 encode_primitive(mut s, value)!
      |                                         ~~~~~
   70 |             }
   71 |         }

Proposed Solution

introduce something like

$if field is shared {
} $else {
}

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.12 ac2e7d7.4b5f337

Environment details (OS name and version, etc.)

V full version V 0.4.12 ac2e7d7.4b5f337
OS linux, Ubuntu 24.04.3 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 11.73GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-10-26 13:12:30
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/mars/v/bug/table
Git version git version 2.43.0
V git status weekly.2025.42-63-gac2e7d7f-dirty
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.6) 2.39

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions