microsoft / typespec

https://typespec.io/
MIT License
4.1k stars 193 forks source link

Decorators with named parameters? #513

Open tjprescott opened 2 years ago

tjprescott commented 2 years ago

Currently, it seems that decorators only accept arguments positionally. I'm wondering if it would be feasible/beneficial to support keyword arguments.

// 1 - Positional makes it hard to know the expected inputs when there are many options
@petDecorator("Fluffy", 10, "orange", "tuna")
...

// 2 - Makes it a lot easier to know what you are setting, and only set those parameters you care about.
@petDecorator(name: "Fluffy", age: 10, color: "orange", favoriteTreat: "tuna")
...

// 3 - Alternatively, you could just have many small decorators.
@petName("Fluffy") @petAge(10) @petColor("orange") @petFavoriteTreat("tuna")
...
bterlson commented 2 years ago

@petDecorator({name: "Fluffy", age: 10 }) works today, I'm not sure named parameters add much on top if this?

tjprescott commented 2 years ago

This syntax works for me. @timotheeguerin do you agree?

timotheeguerin commented 2 years ago

I think my main problem with what we have now is that there is no help and validation of the params is hard. Passing an object is fine, though slightly uglier. We just need to make sure we'll be able to have CADL do type validation on that.(which i think really is dependent on the subtype/supertype issue like a lot of other things)

markcowl commented 10 months ago

related to https://github.com/microsoft/typespec/issues/1955