-
-
Notifications
You must be signed in to change notification settings - Fork 700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement issue# 16744: TypeOf template helper. #4920
Conversation
typeof cannot be used directly with templates such as staticMap, because we don't have the template equivalent of lambda functions. TypeOf is therefore a template wrapper around typeof so that it can be used in contexts that require a template.
How about: /++
Returns:
The type of the expression `exp` or `T`
if the argument is already a type.
Useful when combined with higher-order templates
such as $(REF, staticMap, std, meta).
+/
alias TypeOf(alias exp) = typeof(exp);
/// ditto
alias TypeOf(T) = T;
unittest
{
import std.meta : AliasSeq, staticMap;
static assert(is(TypeOf!1 == int));
static assert(is(TypeOf!"hello" == string));
static assert(is(TypeOf!(17 + 42) == int));
int[] arr;
static assert(is(TypeOf!arr == int[]));
static assert(is(staticMap!(TypeOf, 1, true, "hello") ==
AliasSeq!(int, bool, string)));
struct S {}
static assert(is(staticMap!(TypeOf, 1, true, int, bool, short, S.init, S) ==
AliasSeq!(int, bool, int, bool, short, S, S)));
} In other words:
|
It doesn't return anything, so having a "returns" section just seems wrong to me. The docs what it does, and the examples show it. I could reduce it to the one-liner, and if folks want me to, that's fine, but I don't see much benefit. It works either way. Regardless, I completely disagree with having it work with stuff that |
I don't have a strong opinion about this, though the templates in
In general, I believe that code should pay for itself. I.e. there shouldn't be any unnecessary code. Shorter code is most often easier to read and understand.
I disagree. A generic library should not impose arbitrary restrictions on its users. This is like saying that static assert(SizeOf!(3 + 4) == 4);
static assert(SizeOf!int == 4);
static assert(SizeOf!"asd" == 2 * size_t.sizeof);
static assert(SizeOf!'c' == 1);
template SizeOf(T...)
if (T.length == 1)
{
enum SizeOf = TypeOf!(T[0]).sizeof;
} Why would the writer of |
There is nothing arbitrary about the restriction. |
@@ -77,6 +77,7 @@ | |||
* $(LREF StringTypeOf) | |||
* $(LREF AssocArrayTypeOf) | |||
* $(LREF BuiltinTypeOf) | |||
* $(LREF TypeOf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change to Typeof
so it matches the built-in operator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That wouldn't match the naming conventions or how other templates in std.traits are named.
Helper template so that `typeof can be used with templates taking other | ||
templates (such as $(REF, staticMap, std, meta)). | ||
+/ | ||
template TypeOf(alias exp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is better implemented as @ZombineDev described.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have TypeOf
work on types even though typeof
doesn't work on types? As I said, I don't think that it makes any sense to get the type of a type, and I'm not going to make that change.
@@ -5074,6 +5075,31 @@ template BuiltinTypeOf(T) | |||
else static assert(0); | |||
} | |||
|
|||
/++ | |||
Helper template so that `typeof can be used with templates taking other | |||
templates (such as $(REF, staticMap, std, meta)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plenty of other templates in this module use the word "returns", so please add a Returns:
section here, as well as Params:
. See https://github.com/jmdavis/phobos/blob/95112dc7cabe31ab3b63679d0c8a283d6dfef496/std/traits.d#L2034 for an example.
} | ||
|
||
/// | ||
unittest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a unit test or two for types as well.
How often do we need staticMap with typeof in the standard library? The savings should be considerable; otherwise we can just leave the one-liner as an example. |
I don't follow. Do you want the implementation changed in some way, or do you disagree with the fact that |
@jmdavis the simplest question is: since it's a trivial one-liner, why not just ask the user to do it if they have a need? We need to have a case for high frequency of use. Also: copying |
This an alternative implementation to #4920
This an alternative implementation to #4920
This an alternative implementation to #4920
This an alternative implementation to #4920
This an alternative implementation to #4920
This an alternative implementation to #4920
This an alternative implementation to #4920
This an alternative implementation to #4920
This an alternative implementation to #4920
Closing in favor of #5662 |
typeof cannot be used directly with templates such as staticMap, because
we don't have the template equivalent of lambda functions. TypeOf is
therefore a template wrapper around typeof so that it can be used in
contexts that require a template.