www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Return types of the methods of a struct

reply "Quentin Ladeveze" <ladeveze.quentin openmailbox.org> writes:
Hi,

   I have a struct with some methods int it, let's say

```
struct Example{
    int intValue(){
Jun 19 2015
parent reply "Quentin Ladeveze" <ladeveze.quentin openmailbox.org> writes:
On Friday, 19 June 2015 at 13:12:08 UTC, Quentin Ladeveze wrote:
 Hi,

   I have a struct with some methods int it, let's say

 ```
 struct Example{
    int intValue(){
Hum. How can I delete a post ?
Jun 19 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/19/15 9:13 AM, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 13:12:08 UTC, Quentin Ladeveze wrote:
 Hi,

   I have a struct with some methods int it, let's say

 ```
 struct Example{
    int intValue(){
Hum. How can I delete a post ?
You can't. The forum is backed by a newsgroup, just post the full corrected version :) -Steve
Jun 19 2015
parent reply "Quentin Ladeveze" <ladeveze.quentin openmailbox.org> writes:
On Friday, 19 June 2015 at 13:26:03 UTC, Steven Schveighoffer 
wrote:
 On 6/19/15 9:13 AM, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 13:12:08 UTC, Quentin Ladeveze 
 wrote:
 Hi,

   I have a struct with some methods int it, let's say

 ```
 struct Example{
    int intValue(){
Hum. How can I delete a post ?
You can't. The forum is backed by a newsgroup, just post the full corrected version :) -Steve
Thank you :) Here is th corrected version : Hi, I have a struct with some methods int it, let's say ``` struct Example{ int a(){ return someValue; } float b(){ return someOtherValue; } string stringValue(){ return c; } } Is there any way to have a asTuple method in this struct that would returns something like : Tuple!(int, "a", float, "b", string, "c") and that will contain the values of the methods of the struct ? Thanks.
Jun 19 2015
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/19/15 9:27 AM, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 13:26:03 UTC, Steven Schveighoffer wrote:
 On 6/19/15 9:13 AM, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 13:12:08 UTC, Quentin Ladeveze wrote:
 Hi,

   I have a struct with some methods int it, let's say

 ```
 struct Example{
    int intValue(){
Hum. How can I delete a post ?
You can't. The forum is backed by a newsgroup, just post the full corrected version :) -Steve
Thank you :) Here is th corrected version : Hi, I have a struct with some methods int it, let's say ``` struct Example{ int a(){ return someValue; } float b(){ return someOtherValue; } string stringValue(){ return c; } } Is there any way to have a asTuple method in this struct that would returns something like : Tuple!(int, "a", float, "b", string, "c") and that will contain the values of the methods of the struct ?
Does this work for you, or is there a further expectation? auto asTuple() { return Tuple!(int, "a", ...)(a, b, stringValue);} -Steve
Jun 19 2015
parent reply "Quentin Ladeveze" <ladeveze.quentin openmailbox.org> writes:
On Friday, 19 June 2015 at 13:38:45 UTC, Steven Schveighoffer 
wrote:
 Does this work for you, or is there a further expectation?

 auto asTuple() { return Tuple!(int, "a", ...)(a, b, 
 stringValue);}

 -Steve
In fact, I was trying to use traits to create the tuple automatically and being able to add or remove methods to the struct without breaking the asTuple method. I used allMembers for the name of the methods, but I didn't found anything for their return types or their values. Thank you.
Jun 19 2015
next sibling parent reply "Baz" <bb.temp gmx.com> writes:
On Friday, 19 June 2015 at 13:52:54 UTC, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 13:38:45 UTC, Steven Schveighoffer 
 wrote:
 Does this work for you, or is there a further expectation?

 auto asTuple() { return Tuple!(int, "a", ...)(a, b, 
 stringValue);}

 -Steve
In fact, I was trying to use traits to create the tuple automatically and being able to add or remove methods to the struct without breaking the asTuple method. I used allMembers for the name of the methods, but I didn't found anything for their return types or their values. Thank you.
when the return type is defined inside the function it's called a 'Voldemort type', see http://www.drdobbs.com/cpp/voldemort-types-in-d/232901591?pgno=2
Jun 19 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/19/15 10:01 AM, Baz wrote:
 On Friday, 19 June 2015 at 13:52:54 UTC, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 13:38:45 UTC, Steven Schveighoffer wrote:
 Does this work for you, or is there a further expectation?

 auto asTuple() { return Tuple!(int, "a", ...)(a, b, stringValue);}

 -Steve
In fact, I was trying to use traits to create the tuple automatically and being able to add or remove methods to the struct without breaking the asTuple method. I used allMembers for the name of the methods, but I didn't found anything for their return types or their values. Thank you.
when the return type is defined inside the function it's called a 'Voldemort type', see http://www.drdobbs.com/cpp/voldemort-types-in-d/232901591?pgno=2
Actually, this isn't a voldemort type, because the type can be named outside the function. -Steve
Jun 19 2015
prev sibling parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Fri, 19 Jun 2015 13:52:52 +0000
Quentin Ladeveze via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 On Friday, 19 June 2015 at 13:38:45 UTC, Steven Schveighoffer 
 wrote:
 Does this work for you, or is there a further expectation?

 auto asTuple() { return Tuple!(int, "a", ...)(a, b, 
 stringValue);}

 -Steve
In fact, I was trying to use traits to create the tuple automatically and being able to add or remove methods to the struct without breaking the asTuple method. I used allMembers for the name of the methods, but I didn't found anything for their return types or their values. Thank you.
http://dlang.org/phobos/std_traits.html#ReturnType http://dlang.org/phobos/std_traits.html#ParameterTypeTuple
Jun 19 2015
parent reply "Quentin Ladeveze" <ladeveze.quentin openmailbox.org> writes:
On Friday, 19 June 2015 at 14:04:05 UTC, Daniel Kozák wrote:
 On Fri, 19 Jun 2015 13:52:52 +0000
 Quentin Ladeveze via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 On Friday, 19 June 2015 at 13:38:45 UTC, Steven Schveighoffer 
 wrote:
 Does this work for you, or is there a further expectation?

 auto asTuple() { return Tuple!(int, "a", ...)(a, b, 
 stringValue);}

 -Steve
In fact, I was trying to use traits to create the tuple automatically and being able to add or remove methods to the struct without breaking the asTuple method. I used allMembers for the name of the methods, but I didn't found anything for their return types or their values. Thank you.
http://dlang.org/phobos/std_traits.html#ReturnType http://dlang.org/phobos/std_traits.html#ParameterTypeTuple
These are interesting and can be useful, but allMembers returns strings and not functions, so I can't apply ReturnType.
Jun 19 2015
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/19/15 10:13 AM, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 14:04:05 UTC, Daniel Kozák wrote:
 On Fri, 19 Jun 2015 13:52:52 +0000
 Quentin Ladeveze via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 On Friday, 19 June 2015 at 13:38:45 UTC, Steven Schveighoffer wrote:
 Does this work for you, or is there a further expectation?

 auto asTuple() { return Tuple!(int, "a", ...)(a, b, > stringValue);}

 -Steve
In fact, I was trying to use traits to create the tuple automatically and being able to add or remove methods to the struct without breaking the asTuple method. I used allMembers for the name of the methods, but I didn't found anything for their return types or their values. Thank you.
http://dlang.org/phobos/std_traits.html#ReturnType http://dlang.org/phobos/std_traits.html#ParameterTypeTuple
These are interesting and can be useful, but allMembers returns strings and not functions, so I can't apply ReturnType.
It's a *compile time* string. D is able to do some amazing things with this :) // assuming 'a' is the first member mixin("alias aReturnType = ReturnType!(Example." ~ __traits(allMembers, Example)[0] ~ ");"); static assert(is(aReturnType == int)); Using foreach over allMembers, you can construct a perfect return tuple using introspection and mixin. -Steve
Jun 19 2015
parent reply "Quentin Ladeveze" <ladeveze.quentin openmailbox.org> writes:
On Friday, 19 June 2015 at 14:42:59 UTC, Steven Schveighoffer 
wrote:
 On 6/19/15 10:13 AM, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 14:04:05 UTC, Daniel Kozák wrote:
 On Fri, 19 Jun 2015 13:52:52 +0000
 Quentin Ladeveze via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 On Friday, 19 June 2015 at 13:38:45 UTC, Steven 
 Schveighoffer wrote:
 Does this work for you, or is there a further expectation?

 auto asTuple() { return Tuple!(int, "a", ...)(a, b, > 
 stringValue);}

 -Steve
In fact, I was trying to use traits to create the tuple automatically and being able to add or remove methods to the struct without breaking the asTuple method. I used allMembers for the name of the methods, but I didn't found anything for their return types or their values. Thank you.
http://dlang.org/phobos/std_traits.html#ReturnType http://dlang.org/phobos/std_traits.html#ParameterTypeTuple
These are interesting and can be useful, but allMembers returns strings and not functions, so I can't apply ReturnType.
It's a *compile time* string. D is able to do some amazing things with this :) // assuming 'a' is the first member mixin("alias aReturnType = ReturnType!(Example." ~ __traits(allMembers, Example)[0] ~ ");"); static assert(is(aReturnType == int)); Using foreach over allMembers, you can construct a perfect return tuple using introspection and mixin. -Steve
I would never have thought about mixins, this is amazing ! But thinking at compile time still is a little difficult for me, and now I don't understand how you can construct your tuple. I've been trying to create a string, by iterating on allMembers and concatenating the result of my functions in a string. But of course, I cannot use this string at compile time. Now I know there is a way t do it, but my brain just can't figure it out. Can you help me ?
Jun 19 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/19/15 12:09 PM, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 14:42:59 UTC, Steven Schveighoffer wrote:
 On 6/19/15 10:13 AM, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 14:04:05 UTC, Daniel Kozák wrote:
 On Fri, 19 Jun 2015 13:52:52 +0000
 Quentin Ladeveze via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 On Friday, 19 June 2015 at 13:38:45 UTC, Steven Schveighoffer wrote:
 Does this work for you, or is there a further expectation?

 auto asTuple() { return Tuple!(int, "a", ...)(a, b, > >
stringValue);}
 -Steve
In fact, I was trying to use traits to create the tuple automatically and being able to add or remove methods to the struct without breaking the asTuple method. I used allMembers for the name of the methods, but I didn't found anything for their return types or their values. Thank you.
http://dlang.org/phobos/std_traits.html#ReturnType http://dlang.org/phobos/std_traits.html#ParameterTypeTuple
These are interesting and can be useful, but allMembers returns strings and not functions, so I can't apply ReturnType.
It's a *compile time* string. D is able to do some amazing things with this :) // assuming 'a' is the first member mixin("alias aReturnType = ReturnType!(Example." ~ __traits(allMembers, Example)[0] ~ ");"); static assert(is(aReturnType == int)); Using foreach over allMembers, you can construct a perfect return tuple using introspection and mixin. -Steve
I would never have thought about mixins, this is amazing ! But thinking at compile time still is a little difficult for me, and now I don't understand how you can construct your tuple. I've been trying to create a string, by iterating on allMembers and concatenating the result of my functions in a string. But of course, I cannot use this string at compile time. Now I know there is a way t do it, but my brain just can't figure it out. Can you help me ?
The string must stay compile time. This means it must be calculated: 1. in a mixin statement Example is what I wrote earlier. 2. as an initializer to an immutable, enum, or non-local variable enum x = "Blah." ~ __traits(allMembers, Blah)[0]; // x is compile-time 3. inside a CTFE function. A CTFE function is just a normal runtime function with restrictions (see http://dlang.org/function.html#interpretation). But you must *call* it from a context like 1 or 2: string foo(string a, string b) { return a ~ b; } // foo is CTFEable auto x1 = foo("a", "b"); // x1 is a runtime string, foo is called at runtime enum x2 = foo("a", "b"); // x2 is a compile-time string, foo is executed at compile time (it is not called during execution of your program mixin("int " ~ x1 ~ ";"); // error, can't use runtime string x1 mixin("int " ~ x2 ~ ";"); // ok, declares int ab; mixin("int " ~ foo("c", "d") ~ ";"); // ok, declares int ab; Keep your code all executing at compile time, and you can use your strings to write your code for you! -Steve
Jun 19 2015
prev sibling parent reply "ZombineDev" <valid_email he.re> writes:
On Friday, 19 June 2015 at 14:13:46 UTC, Quentin Ladeveze wrote:
 [..]
These are interesting and can be useful, but allMembers returns strings and not functions, so I can't apply ReturnType.
Here's my solution: http://dpaste.dzfl.pl/c69de3c16d75
Jun 19 2015
parent "Quentin Ladeveze" <ladeveze.quentin openmailbox.org> writes:
On Friday, 19 June 2015 at 15:47:09 UTC, ZombineDev wrote:
 On Friday, 19 June 2015 at 14:13:46 UTC, Quentin Ladeveze wrote:
 [..]
These are interesting and can be useful, but allMembers returns strings and not functions, so I can't apply ReturnType.
Here's my solution: http://dpaste.dzfl.pl/c69de3c16d75
Thank you very much, this solution is interesting too.
Jun 19 2015
prev sibling next sibling parent reply "Baz" <bb.temp gmx.com> writes:
On Friday, 19 June 2015 at 13:27:15 UTC, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 13:26:03 UTC, Steven Schveighoffer 
 wrote:
 On 6/19/15 9:13 AM, Quentin Ladeveze wrote:
 [...]
You can't. The forum is backed by a newsgroup, just post the full corrected version :) -Steve
Thank you :) Here is th corrected version : Hi, I have a struct with some methods int it, let's say ``` struct Example{ int a(){ return someValue; } float b(){ return someOtherValue; } string stringValue(){ return c; } } Is there any way to have a asTuple method in this struct that would returns something like : Tuple!(int, "a", float, "b", string, "c") and that will contain the values of the methods of the struct ? Thanks.
in the declaration set the return type to 'auto'. Use 'tuple' instead of 'Tuple'.
Jun 19 2015
parent "Baz" <bb.temp gmx.com> writes:
On Friday, 19 June 2015 at 13:40:01 UTC, Baz wrote:
 On Friday, 19 June 2015 at 13:27:15 UTC, Quentin Ladeveze wrote:
 On Friday, 19 June 2015 at 13:26:03 UTC, Steven Schveighoffer 
 wrote:
 [...]
Thank you :) Here is th corrected version : Hi, I have a struct with some methods int it, let's say ``` struct Example{ int a(){ return someValue; } float b(){ return someOtherValue; } string stringValue(){ return c; } } Is there any way to have a asTuple method in this struct that would returns something like : Tuple!(int, "a", float, "b", string, "c") and that will contain the values of the methods of the struct ? Thanks.
in the declaration set the return type to 'auto'. Use 'tuple' instead of 'Tuple'.
Ah, damn, haven't see Steven Schveighoffer answer while i was writing. cross post. same thing.
Jun 19 2015
prev sibling parent reply Justin Whear <justin economicmodeling.com> writes:
On Fri, 19 Jun 2015 13:27:13 +0000, Quentin Ladeveze wrote:
 
 Is there any way to have a asTuple method in this struct that would
 returns something like :
 
 Tuple!(int, "a", float, "b", string, "c")
 
 and that will contain the values of the methods of the struct ?
 
 Thanks.
You'll want to work your way through this example carefully as it's basically template-based functional programming, but I think does what you want: http://dpaste.dzfl.pl/b048ea3adb93
Jun 19 2015
parent "Quentin Ladeveze" <ladeveze.quentin openmailbox.org> writes:
On Friday, 19 June 2015 at 15:36:54 UTC, Justin Whear wrote:
 On Fri, 19 Jun 2015 13:27:13 +0000, Quentin Ladeveze wrote:
 
 Is there any way to have a asTuple method in this struct that 
 would returns something like :
 
 Tuple!(int, "a", float, "b", string, "c")
 
 and that will contain the values of the methods of the struct ?
 
 Thanks.
You'll want to work your way through this example carefully as it's basically template-based functional programming, but I think does what you want: http://dpaste.dzfl.pl/b048ea3adb93
This is interesting, thank you very much ! I will try to figure out how it works now
Jun 19 2015