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

Allow anonymous structures to be created on the fly #23855

Closed
2 tasks
gchumillas opened this issue Mar 4, 2025 · 3 comments · Fixed by #23857
Closed
2 tasks

Allow anonymous structures to be created on the fly #23855

gchumillas opened this issue Mar 4, 2025 · 3 comments · Fixed by #23857
Assignees
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@gchumillas
Copy link

gchumillas commented Mar 4, 2025

Describe the feature

Currently you can create "nested" anonymous structures:
https://docs.vlang.io/structs.html#anonymous-structs

Use Case

But you can't create anonymous structures on the fly:

// this code doesn't compile:
book := struct {
	title: 'Allice in Wornderland'
	year: 1865
}

Proposed Solution

Allow to create not only nested anonymous structures but also top-level anonymous structures.

Other Information

V Playground: https://play.vlang.io/p/e7ad7d9486

Acknowledgements

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

Version used

V 0.4.9 a59ebea

Environment details (OS name and version, etc.)

|V full version      |V 0.4.9 a59ebea
|:-------------------|:-------------------
|OS                  |linux, Debian GNU/Linux 12 (bookworm) (VM)
|Processor           |2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
|Memory              |0.62GB/2.02GB
|                    |
|V executable        |/home/admin/v/v
|V last modified time|2025-03-04 09:25:29
|                    |
|V home dir          |OK, value: /home/admin/v
|VMODULES            |OK, value: .vmodules
|VTMP                |OK, value: /tmp/v_0
|Current working dir |OK, value: /home/admin/playground
|                    |
|Git version         |git version 2.39.5
|V git status        |Error: fatal: detected dubious ownership in repository at '/home/admin/v'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v
|.git/config present |true
|                    |
|cc version          |cc (Debian 12.2.0-14) 12.2.0
|gcc version         |gcc (Debian 12.2.0-14) 12.2.0
|clang version       |Debian clang version 14.0.6
|tcc version         |tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
|tcc git status      |Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc

|emcc version        |N/A
|glibc version       |ldd (Debian GLIBC 2.36-9+deb12u8) 2.36

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.

Copy link

Connected to Huly®: V_0.6-22259

@felipensp felipensp added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Mar 4, 2025
@felipensp
Copy link
Member

felipensp commented Mar 4, 2025

Today you can also pass as argument:

fn t(a struct { name string }) {
	dump(a)
}

t(struct { name: 'Foo'})

@jorgeluismireles
Copy link

jorgeluismireles commented Mar 4, 2025

I see even accept attributes

import json

fn t(a struct { name string @[omitempty] }) {
	println(json.encode(a))
}

t(struct { name: 'Foo'}) // {"name":"Foo"}
t(struct { name: ''})    // {}

This works too:

t(name: 'Foo') // {"name":"Foo"}
t(name: '')    // {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants