Replies: 13 comments
-
I 100% agree. |
Beta Was this translation helpful? Give feedback.
-
But I'd keep the current syntax: edit hm that would actually conflict with |
Beta Was this translation helpful? Give feedback.
-
Quotes are also good, so it would look like this? |
Beta Was this translation helpful? Give feedback.
-
Instead of having a system that can break and go unnoticed with a simple typo, we could structure attributes to make them make more sense and be more robust, like in Java, C#, Kotlin, etc. Here is an example of how it could look like: // in json/json.v
// Bodyless attribute
attribute Skip
// Attribute with value
attribute SerialName(string) // in main.v
import json
struct Foo {
bar int [json.SerialName('Bar')]
baz string [json.Skip]
} |
Beta Was this translation helpful? Give feedback.
-
at the end of the day, attribute would be a struct?(ref. to gen) and how would we get the arguments? |
Beta Was this translation helpful? Give feedback.
-
yep I think making attribute a struct would be good: // in json.v
attribute SerialName {
name string
}
attribute Skip {} // in main.v
struct Foo {
bar int [json.SerialName{'Bar'}]
baz string [json.Skip]
} or maybe better: attribute is a reserved struct// in json.v
attribute {
name string
skip bool
required bool
} // in main.v
struct Foo {
bar int [json{name:'Bar'}]
baz string [json{skip:true}]
} |
Beta Was this translation helpful? Give feedback.
-
attribute My_attribute(string, int, f32) well this could be abstracted to a struct, eg... struct Foo {
bar int [My_attribute("test", 1, 1.3)]
baz string
} V would build a struct that way... struct My_attribute {
pub:
value1 string
value2 int
value3 f32
} fn (f Foo) test() {
$for t_ in Test.attributes {
t_ is My_attribute {
....
}
}
} |
Beta Was this translation helpful? Give feedback.
-
it seems simpler |
Beta Was this translation helpful? Give feedback.
-
How about [attribute]
struct SerialName {
name string
}
[attribute]
struct Skip {} it doesn't add special syntax |
Beta Was this translation helpful? Give feedback.
-
I suggested this in the discord server, and i agree. It would be much simpler, but a little verbose for bigger attributes, which won't be common anyway. One big question i have is how will empty attributes be used? edit: the |
Beta Was this translation helpful? Give feedback.
-
Its a weak opinion but I would rather vote for one struct defining all attributes than multiple structs defining multiple attributes, the reason is readability and maintainability. I don't care if it is like attributes {
name string
skip bool
required bool
} or [attributes]
struct Attributes {
name string
skip bool
required bool
}
|
Beta Was this translation helpful? Give feedback.
-
yes, but what about the builtiin attributes, will they remain as they are in a form of use reserved for them? |
Beta Was this translation helpful? Give feedback.
-
Could be like normal attributes: [MarkUsed]
fn foo() {} |
Beta Was this translation helpful? Give feedback.
-
Describe the feature
Structs and Structfields support having attributes, as an example:
It should be required to scope these attributes to the package that defines the behavior and acts on the attribute value.
Example:
Use Case
A simple use case would be using the same struct for orm and json.
Lets say we want to build a simple rest api with CRUD operations, the data should be kept in a database.
We can define a struct the following:
Now that would suit perfect the database, however all json decode operations would require custom logic to ignore the
id
andcreated_on
fields passed in by the client.e.g. the create operation:
Proposed Solution
Instead of having "global" tags, like
skip
orrequired
, it should be mandatory to scope the tag to the handling package:Other Information
An alternative would be that developers create two structs, one for the "wire", and one for the database.
That has these two (major) negatives:
Acknowledgements
Version used
v0.3.3
Environment details (OS name and version, etc.)
Beta Was this translation helpful? Give feedback.
All reactions