www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Possible syntax for multiple opCast functions

reply B Shropshire <B_member pathlink.com> writes:
How about treat the opCast function as if it were a template:

Class A
{
int opCast!(int)() { ... }	// the opCast operator for casting to int
char opCast!(char)() { ... }	// the opCast operator for casting to char
B opCast!(B)() { ... }		// the opCast operator for casting to B
}
Jul 12 2005
next sibling parent reply zwang <nehzgnaw gmail.com> writes:
B Shropshire wrote:
 How about treat the opCast function as if it were a template:
 
 Class A
 {
 int opCast!(int)() { ... }	// the opCast operator for casting to int
 char opCast!(char)() { ... }	// the opCast operator for casting to char
 B opCast!(B)() { ... }		// the opCast operator for casting to B
 }
 
 
I find
 Class A
 {
 int opCast(int) { ... }	// the opCast operator for casting to int
 char opCast(char) { ... }	// the opCast operator for casting to char
 B opCast(B) { ... }		// the opCast operator for casting to B
 }
more intuitive.
Jul 12 2005
parent reply Victor Nakoryakov <nail-mail mail.ru> writes:
zwang wrote:
 B Shropshire wrote:
 
 How about treat the opCast function as if it were a template:

 Class A
 {
 int opCast!(int)() { ... }    // the opCast operator for casting to int
 char opCast!(char)() { ... }    // the opCast operator for casting to 
 char
 B opCast!(B)() { ... }        // the opCast operator for casting to B
 }
I find > Class A > { > int opCast(int) { ... } // the opCast operator for casting to int > char opCast(char) { ... } // the opCast operator for casting to char > B opCast(B) { ... } // the opCast operator for casting to B > } more intuitive.
I find opCast superfluous. Since implicit casts are not allowed anyway (and thankfully) there are no advantages of opCast to artificial method "MyType toMyType();" -- Victor (aka nail) Nakoryakov nail-mail<at>mail<dot>ru Krasnoznamensk, Moscow, Russia
Jul 12 2005
next sibling parent "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 13 Jul 2005 10:41:56 +0400, Victor Nakoryakov <nail-mail mail.ru>  
wrote:
 zwang wrote:
 B Shropshire wrote:

 How about treat the opCast function as if it were a template:

 Class A
 {
 int opCast!(int)() { ... }    // the opCast operator for casting to int
 char opCast!(char)() { ... }    // the opCast operator for casting to  
 char
 B opCast!(B)() { ... }        // the opCast operator for casting to B
 }
I find > Class A > { > int opCast(int) { ... } // the opCast operator for casting to int > char opCast(char) { ... } // the opCast operator for casting to char > B opCast(B) { ... } // the opCast operator for casting to B > } more intuitive.
I find opCast superfluous. Since implicit casts are not allowed anyway (and thankfully) there are no advantages of opCast to artificial method "MyType toMyType();"
I'd argue the only time implicit casts would make some sense would be for creating a type that imitates a basic type. Like for example a big integer struct/class, it would benefit from implicit conversion as it would then be interchangable with the basic type it was imitating. As it is in effect externally the same thing as the type it imitates there is no downside or penalty or problem with allowing implicit conversions for it. Regan
Jul 13 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Victor Nakoryakov wrote:
<snip>
 I find opCast superfluous. Since implicit casts are not allowed anyway 
 (and thankfully) there are no advantages of opCast to artificial method 
 "MyType toMyType();"
Been suggested before. There are lots of different kinds of types - primitives, programmer-defined types, pointers, arrays, functions, delegates ... how would we name the various toType methods? http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/5598 Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jul 13 2005
prev sibling next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
B Shropshire wrote:
 How about treat the opCast function as if it were a template:
 
 Class A
 {
 int opCast!(int)() { ... }    // the opCast operator for casting to int
 char opCast!(char)() { ... }  // the opCast operator for casting to char
 B opCast!(B)() { ... }        // the opCast operator for casting to B
 }
This would indeed be a new syntax. If you're going to do this, why not opt for one that's a bit less redundant? My idea: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/20712 Various ideas: http://www.wikiservice.at/wiki4d/wiki.cgi?FeatureRequestList Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jul 13 2005
prev sibling parent reply Ender KaShae <astrothayne gmail.com> writes:
B Shropshire Wrote:

 How about treat the opCast function as if it were a template:
 
 Class A
 {
 int opCast!(int)() { ... }	// the opCast operator for casting to int
 char opCast!(char)() { ... }	// the opCast operator for casting to char
 B opCast!(B)() { ... }		// the opCast operator for casting to B
 }
 
rather than have multiple cast use functions like toInt, toChar, toB, for derived types use symbols ex: use toInt_ptr for int* or toInt_ar for int[] or toInt_ar2 for int[2] what is needed is implicit conversions
 
Jul 20 2007
next sibling parent reply "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"Ender KaShae" <astrothayne gmail.com> wrote in message 
news:f7r8eg$1dnn$1 digitalmars.com...
<snip>
 rather than have multiple cast use functions like toInt, toChar, toB, for 
 derived types use symbols ex: use toInt_ptr for int* or toInt_ar for int[] 
 or toInt_ar2 for int[2]
Please see my reply to Victor Nakoryakov on this thread. See also my proposal from a while back: http://tinyurl.com/2uadhz Stewart.
Jul 20 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Stewart,

 http://tinyurl.com/2uadhz
bad link
Jul 20 2007
next sibling parent reply "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:ce0a3343c0718c998f7f0cf2d1e news.digitalmars.com...
 http://tinyurl.com/2uadhz
bad link
Works for me at the moment. Stewart.
Jul 20 2007
parent BCS <ao pathlink.com> writes:
Reply to Stewart,

 "BCS" <ao pathlink.com> wrote in message
 news:ce0a3343c0718c998f7f0cf2d1e news.digitalmars.com...
 
 http://tinyurl.com/2uadhz
 
bad link
Works for me at the moment. Stewart.
OK, works now (must be a non D system :)
Jul 20 2007
prev sibling parent "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:ce0a3343c0718c998f7f0cf2d1e news.digitalmars.com...
 http://tinyurl.com/2uadhz
bad link
Does this one work for you? http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=20712 Stewart.
Jul 20 2007
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Ender,

 B Shropshire Wrote:
 
 How about treat the opCast function as if it were a template:
 
 Class A
 {
 int opCast!(int)() { ... }    // the opCast operator for casting to int
 char opCast!(char)() { ... } // the opCast operator for casting to char
 B opCast!(B)() { ... }   // the opCast operator for casting to B
 }
rather than have multiple cast use functions like toInt, toChar, toB, for derived types use symbols ex: use toInt_ptr for int* or toInt_ar for int[] or toInt_ar2 for int[2] what is needed is implicit conversions
?? I don't follow.
Jul 20 2007