www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Style guide is very inconsistent

reply maik klein <maikklein googlemail.com> writes:
I am a very new D user and I almost always try to mirror the 
"official" style guide if one is available.

http://dlang.org/dstyle.html

I have written the following function

template Contains(C...){
   template Any(T...){
     import std.meta: anySatisfy;
     static if(T.length == 0){
       enum Any = false;
     }
     else{
       enum Any = ContainsImpl!T;
     }
     template ContainsImpl(T...){
       enum bool isSameComponent(Comp) = is(Comp == T[0]);
       static if(T.length == 1){
         enum ContainsImpl = anySatisfy!(isSameComponent,C);
       }
       else{
         enum ContainsImpl = anySatisfy!(isSameComponent,C) && 
ContainsImpl!(T[1..$]);
       }
     }
   }
}


No idea how I should type it contains!c.any!T or contains!c.Any!T

Which lead me to have a look at phobos

For example

template Filter(alias pred, TList...)

vs

template anySatisfy(alias F, T...)

Are aliases now written in upper or lower camelCase? Should I use 
T... or TList... for variadics if I can't name them better?

or

private template GenericReplace(args...)

Why are the variadics written in lower case?
Nov 16 2015
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, 16 November 2015 at 14:09:45 UTC, maik klein wrote:
 Why are the variadics written in lower case?
Per the style guide, ===== Eponymous Templates Templates which have the same name as a symbol within that template (and instantiations of that template are therefore replaced with that symbol) should be capitalized in the same way that that inner symbol would be capitalized if it weren't in a template - e.g. types should be PascalCased and values should be camelCased. ===== So, whether an eponymous template is camelCased or PascalCased depends entirely on what its result is. It's quite possible that there are older symbols in Phobos that don't follow those guidelines, but most do. However, there are some where it's not clear which it should be. e.g. std.meta.Filter is a funny one, because it works with any symbol - be it a type or a value - so it could have gone either way, but it was probably targeted primarily at types, so it ended up with its name being PascalCased. But in general, an eponymous template which results in a type is supposed to be PascalCased, whereas the others are supposed to be camelCased. Really, pretty much anything that isn't a type is camelCased, whereas types are PascalCased. It's just that there a few cases that are inherently ambiguous, and there may be some older symbols in Phobos which don't properly follow the current style guide. - Jonathan M Davis
Nov 16 2015
parent reply maik klein <maikklein googlemail.com> writes:
On Monday, 16 November 2015 at 14:26:41 UTC, Jonathan M Davis 
wrote:
 On Monday, 16 November 2015 at 14:09:45 UTC, maik klein wrote:
 [...]
Per the style guide, ===== Eponymous Templates [...]
Okay thanks, but I am still not sure about my example. I am pretty sure that at least "any" should be camel case because it's a value, but what about "Contains"? It's neither a type nor a eponymous template. My guess is that it should also be camel case? So it would be contains!C.any!T ?
Nov 16 2015
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, 16 November 2015 at 16:42:57 UTC, maik klein wrote:
 On Monday, 16 November 2015 at 14:26:41 UTC, Jonathan M Davis 
 wrote:
 On Monday, 16 November 2015 at 14:09:45 UTC, maik klein wrote:
 [...]
Per the style guide, ===== Eponymous Templates [...]
Okay thanks, but I am still not sure about my example. I am pretty sure that at least "any" should be camel case because it's a value, but what about "Contains"? It's neither a type nor a eponymous template. My guess is that it should also be camel case? So it would be contains!C.any!T ?
Okay. Your Any template should clearly be camelCased, because its result is a bool. The same goes for ContainsImpl. Contains is a weird one though. It's pretty abnormal to use a template that's not a type, a function, an eponymous template, or a mixin template. Contains doesn't actually evaluate to anything on its own. It seems like it's just there so that you can have two sets of variadic arguments, which would tend to imply that it should be camelCased as part of any. However, the style guide says that non-eponymous templates should be PascalCased (though that _usually_ only applies to mixin templates), so a strict reading of the style guide would indicate that Contains should be PascalCased. Of course, using a verb like contains is also a bit weird when it's really just holding a list of types so that they can essentially be passed as the first argument to any. So, I'd probably be inclined to rename the templates to something that was closer to what they're actually doing. e.g. TypeList!(T, U, V).containsAny!(W, X, Y) But even that's not all that great, since TypeList is then specifically tied to containsAny even though it doesn't really look like it is at first glance, and the name TypeList is generic enough that it does seem like you might want to reuse it elsewhere when it really couldn't be. Maybe ContainsList!(T, U, V).containsAny(W, X, Y) would be better? I don't know. Coming up with a name for this is hard. What you really want is something like listsIntersect!((T, U, V), (W, X, Y)) or ListsIntersect!(AliasSeq!(T, U, V), AliasSeq!(W, X, Y)) but there's no way to express it that like that in D (the AliasSeqs auto-expand into a single AliasSeq). The requirement for two sets of variadic template arguments just makes this awkward. In any case, a strict reading of the style guide would seem to indicate that Contains should be PascalCased, because it's not an eponymous template, but given how rare what you're trying to do is, I doubt that much of anyone would complain if you used camelCase instead. I'd probably go with PascalCase though. - Jonathan M Davis
Nov 16 2015
parent maik klein <maikklein googlemail.com> writes:
On Monday, 16 November 2015 at 17:47:10 UTC, Jonathan M Davis 
wrote:
 [...]
Thanks for going into so much detail.
Nov 16 2015