Jump to page: 1 24  
Page
Thread overview
Can you do this in D?
Jul 26, 2012
Wes
Jul 26, 2012
bearophile
Jul 26, 2012
Wes
Jul 26, 2012
bearophile
Jul 26, 2012
Christophe Travert
Jul 26, 2012
Andrej Mitrovic
JavaScript is Wonderous (Was: Can you do this in D?)
Jul 26, 2012
Nick Sabalausky
Jul 26, 2012
H. S. Teoh
Jul 27, 2012
Nick Sabalausky
Jul 26, 2012
Brad Anderson
Jul 26, 2012
Jacob Carlborg
Jul 30, 2012
captaindet
Jul 31, 2012
captaindet
Jul 26, 2012
Jacob Carlborg
Jul 26, 2012
Wes
Jul 26, 2012
Jacob Carlborg
Jul 27, 2012
David Nadlinger
Jul 26, 2012
David Piepgrass
Jul 27, 2012
Nick Sabalausky
Jul 26, 2012
Araq
Jul 26, 2012
David Nadlinger
Jul 26, 2012
Araq
Jul 26, 2012
Simen Kjaeraas
Jul 26, 2012
Walter Bright
Jul 27, 2012
David Nadlinger
Jul 27, 2012
Walter Bright
Jul 26, 2012
Walter Bright
Jul 27, 2012
Wes
Jul 27, 2012
Walter Bright
Jul 27, 2012
Jacob Carlborg
Jul 27, 2012
Jacob Carlborg
July 26, 2012
Can you do this in D? If not then what other language?

1. Can you get a list of all classes and variables in the current
scope e.g. __traits()?

2. Can you statically iterate over every field of a class?
It seems like the only returns are string lists from __traits().
I basically am interested in being able to generate a
PrettyPrint/Serializable/Hash template without passing in every
single field.

3. Is there any way of executing code or programs during compile
time?
I've seen an example of CTFE (Compile Time Function Evaluation),
although I'm unsure if this works for stuff like classes.
However, I am considering more advanced execution (not constants)
such as printing to a file during compiling for stuff like how
long compiling a certain function/template takes.

4. Is there anything like Go/Erlang channels? (synchronous queues)
I assume that synchronized blocks with a shared global dynamic
array would be the solution without the special syntax sugar?

5. Why not support other operators like $, #, and @?
This is more of a rhetorical... as I know the language doesn't
need them, nor would I know if they would be binary/unary
prefix/etc or the precedence... although they would be nice to
have. Specifically I'd like $prefix to be stringification.
July 26, 2012
Wes:

> Can you do this in D? If not then what other language?

Maybe CommonLisp and Scheme (and maybe Clojure) are able to do what you ask for (but normally they don't have the stringification with $).


> 1. Can you get a list of all classes and variables in the current scope e.g. __traits()?

I think it's a missing feature. But what are the use cases? In Python it's easy to do, but it's not a common need.


> 2. Can you statically iterate over every field of a class?
> It seems like the only returns are string lists from __traits().
> I basically am interested in being able to generate a
> PrettyPrint/Serializable/Hash template without passing in every
> single field.

With their names and mixin() then maybe you able to do what you look for.


> 3. Is there any way of executing code or programs during compile
> time?

Only CTFE.


> I've seen an example of CTFE (Compile Time Function Evaluation),
> although I'm unsure if this works for stuff like classes.

CTFE now works with classes too.


> However, I am considering more advanced execution (not constants)
> such as printing to a file during compiling for stuff like how
> long compiling a certain function/template takes.

With the proposed __ctWrtiteln you are able to print things at compile time to standard output. But there is no enough introspection to know how much time it takes to compile part of a program.


> 5. Why not support other operators like $, #, and @?
> This is more of a rhetorical... as I know the language doesn't
> need them, nor would I know if they would be binary/unary
> prefix/etc or the precedence... although they would be nice to
> have. Specifically I'd like $prefix to be stringification.

The operator overloading syntax is string-based, so adding new operators is not too much hard. But I don't know what are the advantages and disadvantages. The $ operator is used (in a certain context) to denote the length of collections.

Bye,
bearophile
July 26, 2012
On 2012-07-26 13:20, Wes wrote:
> Can you do this in D? If not then what other language?

I'm pretty sure you can do all this using Ruby.

> 1. Can you get a list of all classes and variables in the current
> scope e.g. __traits()?

In general, no. But you can get a list of all members in a module or class scope. Just use __traits(allMembers, ...);

> 2. Can you statically iterate over every field of a class?
> It seems like the only returns are string lists from __traits().
> I basically am interested in being able to generate a
> PrettyPrint/Serializable/Hash template without passing in every
> single field.

Yes, if __traits doesn't work it works with .tupleof. For serialization Orange is available:

https://github.com/jacob-carlborg/orange

> 3. Is there any way of executing code or programs during compile
> time?
> I've seen an example of CTFE (Compile Time Function Evaluation),
> although I'm unsure if this works for stuff like classes.
> However, I am considering more advanced execution (not constants)
> such as printing to a file during compiling for stuff like how
> long compiling a certain function/template takes.

Classes should work. It's not possible to print to a file.

> 4. Is there anything like Go/Erlang channels? (synchronous queues)
> I assume that synchronized blocks with a shared global dynamic
> array would be the solution without the special syntax sugar?

I'm not sure. but you could have a look at std.concurrency. std.parallelism might be of interest as well.

This seems to good to read as well: http://www.informit.com/articles/article.aspx?p=1609144

-- 
/Jacob Carlborg
July 26, 2012
> I think it's a missing feature. But what are the use cases? In Python it's easy to do, but it's not a common need.
True, I suppose. It just seems like the programmer could have access to some sort of associative array, where you could have multiple stored values per key sorted by scope.

> With their names and mixin() then maybe you able to do what you look for.
So basically there's no easy way to make a prettyprint like:
foreach(field;__traits(allFields, myClass)) { str ~= field; }

> With the proposed __ctWrtiteln you are able to print things at compile time to standard output. But there is no enough introspection to know how much time it takes to compile part of a program.
Assuming this is the same as #pragma (msg, ""). I just meant actually calling code that runs at compile time that isn't a static-ly defined constant.

> The operator overloading syntax is string-based, so adding new operators is not too much hard. But I don't know what are the advantages and disadvantages. The $ operator is used (in a certain context) to denote the length of collections.
I'm not sure what you mean. Do you mean I can go edit the open source compiler and add in my own language feature? Or does the ability to add a $/@ operator already exist?

July 26, 2012
> In general, no. But you can get a list of all members in a module or class scope. Just use __traits(allMembers, ...);
The allMembers example was lacking fields. It only showed methods, so I wasn't really sure. The problem with it was that it returned strings. I suppose I can still use them in a mixin.

> Yes, if __traits doesn't work it works with .tupleof. For serialization Orange is available:
>
> https://github.com/jacob-carlborg/orange
Awesome. Definitely sounds like what I'm looking for.
July 26, 2012
Wes:

> So basically there's no easy way to make a prettyprint like:
> foreach(field;__traits(allFields, myClass)) { str ~= field; }

I am saying that probably there is a way, using mixins.


> Assuming this is the same as #pragma (msg, "").

It's not the same. It's usable just like a writeln, but in CTFE too. pragma(msg) has a different behavour, and happens in a different compilation stage.


> I'm not sure what you mean. Do you mean I can go edit the open source compiler and add in my own language feature? Or does the ability to add a $/@ operator already exist?

I mean that D compiler writers don't need to introduce new syntax to add that feature. But I don't see lot of people asking for it.

Bye,
bearophile
July 26, 2012
"bearophile" , dans le message (digitalmars.D:173297), a écrit :
>> I'm not sure what you mean. Do you mean I can go edit the open source compiler and add in my own language feature? Or does the ability to add a $/@ operator already exist?
> 
> I mean that D compiler writers don't need to introduce new syntax to add that feature. But I don't see lot of people asking for it.

The $ prefix operator *is* a new syntax. It impacts the lexer and parser and they would have to create new precedence rules. The only think that already exists is the syntax to overload such operator, if it existed.

I don't see anything like new operators comming in the langage before long.

-- 
Christophe
July 26, 2012
On 2012-07-26 14:17, Wes wrote:
>> In general, no. But you can get a list of all members in a module or
>> class scope. Just use __traits(allMembers, ...);
> The allMembers example was lacking fields. It only showed methods, so I
> wasn't really sure. The problem with it was that it returned strings. I
> suppose I can still use them in a mixin.

If you want to access the fields of a class or struct you can use .tupleof:

class Foo { int a; }
Foo.tupleof;

You can have a look how this is used in my serialization library Orange:

https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L188

Interesting templates/functions would be:

* hasField
* fieldsOf
* TypeOfField
* nameOfFieldAt
* setValueOfField
* getValueOfField

-- 
/Jacob Carlborg
July 26, 2012
On 7/26/12 7:44 AM, bearophile wrote:
> Wes:
>
>> Can you do this in D? If not then what other language?
>
> Maybe CommonLisp and Scheme (and maybe Clojure) are able to do what you
> ask for (but normally they don't have the stringification with $).
>
>
>> 1. Can you get a list of all classes and variables in the current
>> scope e.g. __traits()?
>
> I think it's a missing feature. But what are the use cases? In Python
> it's easy to do, but it's not a common need.

What happened to __traits(allMembers, module_name)? It doesn't list the contents of the current scope, but instead that of a module (including the current one).

>> 2. Can you statically iterate over every field of a class?
>> It seems like the only returns are string lists from __traits().
>> I basically am interested in being able to generate a
>> PrettyPrint/Serializable/Hash template without passing in every
>> single field.
>
> With their names and mixin() then maybe you able to do what you look for.
>
>
>> 3. Is there any way of executing code or programs during compile
>> time?
>
> Only CTFE.

The OP is likely new to the community, so throwing acronyms around may not be helpful.

CTFE stands for Compile-Time Function Evaluation. In short you get to evaluate a subset of D during compilation if you force a function call in a static evaluation context, such as initializing an enum or a static.


Andrei
July 26, 2012
On 7/26/12, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote:
> The OP is likely new to the community, so throwing acronyms around may not be helpful.

http://dlang.org/glossary.html

Now if someone would stop fiddling with the damn javascript which keeps erroring out.. I'm beginning to join the Nick camp w.r.t. JS. :p
« First   ‹ Prev
1 2 3 4