View mode: basic / threaded / horizontal-split · Log in · Help
March 30, 2011
__traits so long and ugly, what about ::?
I think :: is not used in the language.

In a recent article about D I saw:

mixin(__traits(identifier, T) ~ " " ~
      to!string(tolower(__traits(identifier, T)[0])) ~
      __traits(identifier, T)[1..$] ~ ";");

What if foo::bar were an alias for __traits(foo, bar) ?

The code would look like this:

mixin(T::identifier ~ " " ~
      to!string(tolower(T::identifier[0])) ~
      T::identifier[1..$] ~ ";");

What do you think?

Another uses:

__traits(int, isArithmetic) ==> int::isArithmetic
__traits(C, isAbstractClass) ==> C::isAbstractClass

__traits(hasMember, S, "m") ==> S::hasMember("m")

Well, you get the idea...

:: might be applied to other compile time uses, but I just came up with 
this...
March 30, 2011
Re: __traits so long and ugly, what about ::?
You are not the only one to find __traits ugly – although not directly 
related to your question, it has been proposed several times to ditch 
is(typeof()) and __traits and replace them with a magic »meta« 
namespace. There is even a bug report about it: 
http://d.puremagic.com/issues/show_bug.cgi?id=3702

David
March 30, 2011
Re: __traits so long and ugly, what about ::?
On 3/30/11 4:32 PM, David Nadlinger wrote:
> You are not the only one to find __traits ugly – although not directly
> related to your question, it has been proposed several times to ditch
> is(typeof()) and __traits and replace them with a magic »meta«
> namespace. There is even a bug report about it:
> http://d.puremagic.com/issues/show_bug.cgi?id=3702
>
> David

David, thanks for the answer. I am aware of that proposal, but I don't 
like it because it's not very readble. You have to read "meta 
isArithmetic int", kind of like how yoda speaks, but in the other case, 
"int::isArithmetic" is much more natural and shorter and nicer.

I'm just suggesting a syntax for accessing compile-time things. It seems 
:: is unused...
March 30, 2011
Re: __traits so long and ugly, what about ::?
On Mar 31, 11 03:28, Ary Manzana wrote:
> I think :: is not used in the language.
>
> In a recent article about D I saw:
>
> mixin(__traits(identifier, T) ~ " " ~
> to!string(tolower(__traits(identifier, T)[0])) ~
> __traits(identifier, T)[1..$] ~ ";");
>
> What if foo::bar were an alias for __traits(foo, bar) ?
>
> The code would look like this:
>
> mixin(T::identifier ~ " " ~
> to!string(tolower(T::identifier[0])) ~
> T::identifier[1..$] ~ ";");
>
> What do you think?
>
> Another uses:
>
> __traits(int, isArithmetic) ==> int::isArithmetic
> __traits(C, isAbstractClass) ==> C::isAbstractClass
>

You've got the order wrong.

> __traits(hasMember, S, "m") ==> S::hasMember("m")
>
> Well, you get the idea...
>
> :: might be applied to other compile time uses, but I just came up with
> this...

-1.

This is confusing as :: is used to separate scopes in C++ (and PHP too). 
e.g.

    struct A {
       int x;
       bool isSame(const A other) pure const { return x == other.x; }
    }

    void main () {
       A a = A(2), b = A(2);
       assert (  a.isSame(b));   // ok
       assert (! a::isSame(b));  // ???
    }

(How about that 'meta' namespace proposal? meta.hasMember(S, "m") )
March 30, 2011
Re: __traits so long and ugly, what about ::?
"Ary Manzana" <ary@esperanto.org.ar> wrote in message 
news:in0169$5sb$1@digitalmars.com...
> On 3/30/11 4:32 PM, David Nadlinger wrote:
>> You are not the only one to find __traits ugly - although not directly
>> related to your question, it has been proposed several times to ditch
>> is(typeof()) and __traits and replace them with a magic »meta«
>> namespace. There is even a bug report about it:
>> http://d.puremagic.com/issues/show_bug.cgi?id=3702
>>
>> David
>
> David, thanks for the answer. I am aware of that proposal, but I don't 
> like it because it's not very readble. You have to read "meta isArithmetic 
> int", kind of like how yoda speaks, but in the other case, 
> "int::isArithmetic" is much more natural and shorter and nicer.
>
> I'm just suggesting a syntax for accessing compile-time things. It seems 
> :: is unused...

I've always felt the meta namespace should be member-call syntax: 
int.meta.whatever

Your suggestion is a nice shortcut to that, though.
March 30, 2011
Re: __traits so long and ugly, what about ::?
On 30/03/2011 20:28, Ary Manzana wrote:
> I think :: is not used in the language.
>
> In a recent article about D I saw:
>
> mixin(__traits(identifier, T) ~ " " ~
> to!string(tolower(__traits(identifier, T)[0])) ~
> __traits(identifier, T)[1..$] ~ ";");
>
> What if foo::bar were an alias for __traits(foo, bar) ?
>
> The code would look like this:
>
> mixin(T::identifier ~ " " ~
> to!string(tolower(T::identifier[0])) ~
> T::identifier[1..$] ~ ";");
>
> What do you think?
>
> Another uses:
>
> __traits(int, isArithmetic) ==> int::isArithmetic
> __traits(C, isAbstractClass) ==> C::isAbstractClass
>
> __traits(hasMember, S, "m") ==> S::hasMember("m")
>
> Well, you get the idea...
>
> :: might be applied to other compile time uses, but I just came up with
> this...

Hmn, interesting idea! I noticed, however, that the "foo" and "bar" in 
your example are actually swapped...

__traits(foo, bar) ==> foo::bar

should be

__traits(foo, bar) ==> bar::foo

For many the uses of __traits I can recall off the top of my head, it 
certainly seems to be both elegant and intuitive. One sore thumb would 
be __traits(compiles, ...) which I believe gets used quite frequently in 
Phobos.

Clarifications and my lack of insight into the complete breadth of 
__traits's capabilities and variations aside, I think this is the best 
suggestion for the replacement of the much maligned experimental keyword 
that I have seen posted since Don's suggestion of a magic namespace[1]. 
I'm not 100% sure if this :: syntax could also tidy up is() expressions 
too, but if it can, I can imagine it having a lot of support. If 
language design was a democracy, I'd certainly vote for it ^^

A...

[1] Search the NG for "Proposal: Replace __traits and is(typeof(XXX)) 
with a 'magic namespace'."
March 30, 2011
Re: __traits so long and ugly, what about ::?
On 30/03/2011 21:09, Alix Pexton wrote:
> On 30/03/2011 20:28, Ary Manzana wrote:
>> I think :: is not used in the language.
>>
>> In a recent article about D I saw:
>>
>> mixin(__traits(identifier, T) ~ " " ~
>> to!string(tolower(__traits(identifier, T)[0])) ~
>> __traits(identifier, T)[1..$] ~ ";");
>>
>> What if foo::bar were an alias for __traits(foo, bar) ?
>>
>> The code would look like this:
>>
>> mixin(T::identifier ~ " " ~
>> to!string(tolower(T::identifier[0])) ~
>> T::identifier[1..$] ~ ";");
>>
>> What do you think?
>>
>> Another uses:
>>
>> __traits(int, isArithmetic) ==> int::isArithmetic
>> __traits(C, isAbstractClass) ==> C::isAbstractClass
>>
>> __traits(hasMember, S, "m") ==> S::hasMember("m")
>>
>> Well, you get the idea...
>>
>> :: might be applied to other compile time uses, but I just came up with
>> this...
>
> Hmn, interesting idea! I noticed, however, that the "foo" and "bar" in
> your example are actually swapped...
>
> __traits(foo, bar) ==> foo::bar
>
> should be
>
> __traits(foo, bar) ==> bar::foo
>
> For many the uses of __traits I can recall off the top of my head, it
> certainly seems to be both elegant and intuitive. One sore thumb would
> be __traits(compiles, ...) which I believe gets used quite frequently in
> Phobos.
>
> Clarifications and my lack of insight into the complete breadth of
> __traits's capabilities and variations aside, I think this is the best
> suggestion for the replacement of the much maligned experimental keyword
> that I have seen posted since Don's suggestion of a magic namespace[1].
> I'm not 100% sure if this :: syntax could also tidy up is() expressions
> too, but if it can, I can imagine it having a lot of support. If
> language design was a democracy, I'd certainly vote for it ^^
>
> A...
>
> [1] Search the NG for "Proposal: Replace __traits and is(typeof(XXX))
> with a 'magic namespace'."

Damn I type slow ><
March 30, 2011
Re: __traits so long and ugly, what about ::?
On 30/03/2011 20:45, KennyTM~ wrote:
> This is confusing as :: is used to separate scopes in C++ (and PHP too).

The first thing it reminded me of was Lua, where a single colon makes 
the left hand side into the first argument of the function on the right.

foo:bar(x) ==> bar.(foo, x)

So it felt kinda familiar to me ^^

A...
March 30, 2011
Re: __traits so long and ugly, what about ::?
On Mar 31, 11 04:19, Alix Pexton wrote:
> On 30/03/2011 20:45, KennyTM~ wrote:
>> This is confusing as :: is used to separate scopes in C++ (and PHP too).
>
> The first thing it reminded me of was Lua, where a single colon makes
> the left hand side into the first argument of the function on the right.
>
> foo:bar(x) ==> bar.(foo, x)
>
> So it felt kinda familiar to me ^^
>
> A...

That is almost like UFCS in D.

    int foo(string x, int y) { return x.length - y; }

    assert (foo("testing", 3) == 4);
    assert ("testing".foo(3) == 4);

But OP's proposal is restricted to __traits only.

__traits is a relatively advanced part of the language, plus many of its 
features has already been exposed via the library std.traits, e.g. 
std.traits.hasMember!(S, "m"), I don't think it really needs a very 
short syntax.
March 30, 2011
Re: __traits so long and ugly, what about ::?
On 3/30/11 5:52 PM, KennyTM~ wrote:
> On Mar 31, 11 04:19, Alix Pexton wrote:
>> On 30/03/2011 20:45, KennyTM~ wrote:
>>> This is confusing as :: is used to separate scopes in C++ (and PHP too).
>>
>> The first thing it reminded me of was Lua, where a single colon makes
>> the left hand side into the first argument of the function on the right.
>>
>> foo:bar(x) ==> bar.(foo, x)
>>
>> So it felt kinda familiar to me ^^
>>
>> A...
>
> That is almost like UFCS in D.
>
> int foo(string x, int y) { return x.length - y; }
>
> assert (foo("testing", 3) == 4);
> assert ("testing".foo(3) == 4);
>
> But OP's proposal is restricted to __traits only.
>
> __traits is a relatively advanced part of the language, plus many of its
> features has already been exposed via the library std.traits, e.g.
> std.traits.hasMember!(S, "m"), I don't think it really needs a very
> short syntax.

Look, metaprogramming in Ruby is a relatively advanced part of the 
language. And you know why it is heavily used and everyone can jump and 
start using it in a matter of seconds and build the most amazing things? 
Because it's very, very, very, (add 1000 very words here), very easy to use.

Why make *anything* hard to use if you can do it in an easier way?
« First   ‹ Prev
1 2 3 4
Top | Discussion index | About this forum | D home