3 ms·
> Use [] instead of <> for generics. It will save you a lot of avoidable trouble down the road. D uses !() for generics. It looks odd at first, but soon become
by WalterBright 7y ago
> Use [] instead of <> for generics. It will save you a lot of avoidable trouble down the road.
D uses !() for generics. It looks odd at first, but soon becomes completely natural:
struct S(T) { T t; }
auto a = S!(int);
auto b = S!int; <= when only one argument is needed
Since ! is not a conventional binary operator, this:
1. parses without lookahead and ambiguity issues
2. stands out in the code as being a template instantiation
Well over a decade of experience with it (and D code typically makes heavy use of templates) with no issues shows that it works.
- imtringued 7y agoFrom a computer science standpoint it is obvious that generic declarations are just functions that accept type parameters and return a declaration according to the type parameters. So the only reasonable suggestion would be to use the same syntax for both type parameters and normal parameters and optionally add something that distinguishes between the two (like your !). Meanwhile picking either <> or [] feels like that decision is based on personal preference. Obviously, you can justify choosing the most popular syntax because of familiarity but then [] was never an option to begin with.
- WalterBright 7y ago> From a computer science standpoint it is obvious that generic declarations are just functions that accept type parameters I'm ashamed that realization was slow in coming to me. But once it did, it was key to greatly simplifying the D template syntax.