www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can operators "return" type?

reply "Zhenya" <zheny list.ru> writes:
Hi!
It would useful for some my project,if operators could be a 
template,that "return"
type.Something like

alias TypeTuple!(int,char) types;

static assert(types[1] == char) //opIndex

So can I define something like that?
Nov 29 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Zhenya:

 It would useful for some my project,if operators could be a 
 template,that "return"
 type.

D operators are functions, and D functions return values. And in D types are not values (unlike Python and several other languages), unless you use a Typeinfo or something. Maybe if you explain better what you are trying to do someone will be able to suggest you an alternative solution. D is not as flexible as Lisp, but it doesn't lack meta-programming/type-processing capabilities. Bye, bearophile
Nov 29 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2012-11-29, 17:33, Zhenya wrote:

 Hi!
 It would useful for some my project,if operators could be a  
 template,that "return"
 type.Something like

 alias TypeTuple!(int,char) types;

 static assert(types[1] == char) //opIndex

 So can I define something like that?

This works out of the box. If you want to define your own TypeTuple-like construct, the answer is most likely going to be no. -- Simen
Nov 29 2012
prev sibling next sibling parent "Zhenya" <zheny list.ru> writes:
On Thursday, 29 November 2012 at 16:55:01 UTC, bearophile wrote:
 Zhenya:

 It would useful for some my project,if operators could be a 
 template,that "return"
 type.

D operators are functions, and D functions return values. And in D types are not values (unlike Python and several other languages), unless you use a Typeinfo or something. Maybe if you explain better what you are trying to do someone will be able to suggest you an alternative solution. D is not as flexible as Lisp, but it doesn't lack meta-programming/type-processing capabilities. Bye, bearophile

Of course I understand,that type is not value in D.I meant that maybe we can declare non-function template operators.For example: struct MyIntType { alias int type; template opSlice() { alias type opSlice; }
Nov 29 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Zhenya:

 For example:

 struct MyIntType
 {
 alias int type;
 template opSlice()
 {
 alias type opSlice;
 }

As you guess, this is not supported in D. Bye, bearophile
Nov 29 2012
prev sibling parent "Zhenya" <zheny list.ru> writes:
On Thursday, 29 November 2012 at 21:53:20 UTC, bearophile wrote:
 Zhenya:

 For example:

 struct MyIntType
 {
 alias int type;
 template opSlice()
 {
 alias type opSlice;
 }

As you guess, this is not supported in D. Bye, bearophile

Thank you,understood(
Nov 29 2012