Skip to content

Proposal for typed attributes #7014

@danieldaeschle

Description

@danieldaeschle

This would be super useful for our vweb framework which has some attributes. Using this technique we could remove compiler magic for the vweb module and make a more generic solution.

Requirements

  1. It should be able to define methods on it. Those methods can be looked up at compile time then and be called.
  2. It should be able to contain fields/parameters.
  3. It should not be able to pass an attribute as a type parameter in function arguments nor cast or instanitate something.
  4. It should not have any field visibility like pub or mut

A: struct attribute - attribute

an attribute struct shouldn't have struct embedding, pub mut, be initialized nor be used as a type parameter.
An example would look like:

enum HttpMethod { get post put delete patch }

[attribute]
struct Route {
    path       string
    method HttpMethod
}

// .get looks a little bit weird. we could use also a string with `'GET'` or something. but it's only an example here.
[Route{'/index', .get}] // or even [Route{path: '/index', method: .get}]
fn index() {}

B: attribute keyword

This would introduce another keyword but would be good as well is:

enum HttpMethod { get post put delete patch }

attribute Route(path string, method HttpMethod)

// .get looks a little bit weird. we could use also a string with `'GET'` or something. but it's only an example here.
[Route('/index', .get)] // or even [Route(path: '/index', method: .get)]
fn index() {}

Obviously, the last propsal 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 castraed form of a struct, we could use the syntax [Route{'/index', .get}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature/Enhancement RequestThis issue is made to request a feature or an enhancement to an existing one.Type: SpecificationEverything related to the formal specification of how V should behave

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions