Proposal for typed attributes #11732
Replies: 25 comments 4 replies
-
Since attributes kinda remind me of TypeScript's decorators and their proposal in ES6, why not borrow from them outright and change it a little? // Most identifier/keyowrd names are short - so I decided to stick with it.
attr fn(f fn()) Route(path string, method HttpMethod) {
router.register(
path: path
method: method
callback: f
)
} Treading attribute functions like they had a context or were a method might allow to specify where and when attributes would be allowed - but that is probably way over the top. Also if I recall correctly, in Rust, you have to specifically import macros as well to use them. With attributes, it should probably be the same to be on the save side to make sure two attributes from different modules would not clash. import webframework
import attrs webframework // explicitly import the webframework attributes. |
Beta Was this translation helpful? Give feedback.
-
@IngwiePhoenix Good idea. Python also has decorators. But decorators are not the same as attributes. Take a look a t C# attributes to see something similar. |
Beta Was this translation helpful? Give feedback.
-
Wouldn't this make the language bigger? |
Beta Was this translation helpful? Give feedback.
-
Alex gaves his ok for it. It just needs a solid specfication. |
Beta Was this translation helpful? Give feedback.
-
You can already implement custom attributes via V's compile-time |
Beta Was this translation helpful? Give feedback.
-
@spaceface777 I will extend this. I forgot to add the definition that those custom attributes are typed. |
Beta Was this translation helpful? Give feedback.
-
I see some flaws if we have untyped attributes:
[get]
['/myroute']
[/route-without-string]
[inline]
pub fn (mut app App) my_route() Result {
return app.text('My Route')
} this results in: having the follwong routes:
|
Beta Was this translation helpful? Give feedback.
-
Also, functionally, what's the difference between an attribute with a string ( Type checking could still be done by the user at compile-time, if we had (for example) a compile-time |
Beta Was this translation helpful? Give feedback.
-
I see an advantage to define Non-typed attributes wouldn't give the ability to have middlewares in attributes: [attribute]
struct Authorize {}
pub fn (a Authorize) before_request() {
// some jwt stuff and return 401 if not authorized
}
[Route('/settings', .get)]
[Authorize]
pub fn (mut app App) settings() {
// ...
} It would be the same as C# has. Very powerful. |
Beta Was this translation helpful? Give feedback.
-
It's a bug, not a different functionality. It's just not easy to catch all cases which the user can do wrong. With typed attributes compile time check would be easier. |
Beta Was this translation helpful? Give feedback.
-
yeah, ok, I see your point and agree :) |
Beta Was this translation helpful? Give feedback.
-
Well, do I understand it correctly, that an attribute |
Beta Was this translation helpful? Give feedback.
-
I don't really like the [square bracket] notation, sorry. I don't plan to learn C#, where I guess many got used to it. Makes it seem too 'magical', & I've been programed to see everything in a square bracket as a list. I'd prefer more plain text notation like IngwiePhoenix's idea. The idea of 'attributes' would make things like vweb much easier to program for sure. Maybe make it a DSL for vweb, & see if you like it enough for all of V? |
Beta Was this translation helpful? Give feedback.
-
@larpon why close and reopen? :D @tomByrer Yeah, it's your personal preference. But does it matter if it's This issue is more about having typed attributes rather than chaning the attribute feature. What IngwiePhoenix showed are not attributes. That are decorators which is a different technologie. |
Beta Was this translation helpful? Give feedback.
-
Yes - and no. Let's use Say the attr fn(mut f &FunctionData) extern(name string) {
// ...
}
They are not exactly the same, but basically work on even grounds. Just that in C But if you use |
Beta Was this translation helpful? Give feedback.
-
Yeah, I know everything you mentioned. My understanding is that V has no decorators like Python or TypeScript but attributes like C# which are handled at compiletime which is faster. |
Beta Was this translation helpful? Give feedback.
-
Yeah, right now all attributes are handled at compile time and in the generator - cgen, for instance. Technically, if the comptime feature was extended to define compiletile functions, attributes defined per library wouldn't be far off. |
Beta Was this translation helpful? Give feedback.
-
We won't use compile time functions for this case. This doesn't work. Compile time function don't have access to struct fields. |
Beta Was this translation helpful? Give feedback.
-
That's true! I'm shopping for a language with few 'magic characters'; faster to learn & teach IMHO, & I don't have to think twice if [] = array or not. I do like |
Beta Was this translation helpful? Give feedback.
-
I think it is related: #11903 |
Beta Was this translation helpful? Give feedback.
-
So... Whatever became of this proposal? I want to write a web app with V and would love to use attributes to denote routes and possibly other things. Any news? |
Beta Was this translation helpful? Give feedback.
-
Hello, is there anything new about this? Is this discarded or will it be implemented in the future? 🤔 |
Beta Was this translation helpful? Give feedback.
-
I don't like this feature. I never liked it in C# either. This is a hidden code generation and not obvious to the casual code reader what it does. The language is less readable in terms of understanding what is there, it is not translatable in obvious ways, for example, in porting code. |
Beta Was this translation helpful? Give feedback.
-
suggesting a syntax like this
function attribute call automatically when an instance with an attribute is created, an attribute object can be accessible in a function |
Beta Was this translation helpful? Give feedback.
-
Completely agree. I will use it if it's there but I would prefer if I did not have to learn another thing. It is more flexible to register the callbacks programmatically in any fashion you'd like than to be locked down to a special purpose typing. In C# this is a complete clusterfuck too where multi-line decorators is the norm, so I do not think looking at C# as best practice is good. At best Python and flask/Django could serve as a prototype, but even there, it's convenient boilerplate for hello-world sized services. It is not needed unless there is a number of other use cases from say Python that V also want to cater to. |
Beta Was this translation helpful? Give feedback.
-
This would be super useful for our
vweb
framework which has some attributes. Using this technique, we could remove compiler magic for thevweb
module and make a more generic solution.Requirements
pub
ormut
A: struct attribute -
attribute
B: attribute keyword
This would introduce another keyword but would be good as well is:
Obviously, the last proposal would introduce more complexity to the compiler which could also slow it down. But the syntax will be shorter and structs wouldn't be used for different things.
Using attributes
Either attribute using is always like this:
@[Route('/index', .get)]
.Or if we decide to use a casted form of a struct, we could use the syntax
@[Route{'/index', .get}]
Beta Was this translation helpful? Give feedback.
All reactions