Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 06, 2005 [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
What do you guys think of the C# 'as' operator? It's essentially a more aesthetic way to make a cast, but if incorporated into D, it could be made different. For example: # void func(Foo foo) { # // You can do: # Bar bar = foo as Bar; # bar.someBarFunc(); # # // Or even the highly effective: # (foo as Bar).someBarFunc(); # } You've got to admit it's a much cleaner syntax. Note: I believe in C# this is an Exception-safe operation, and it will return null on failure. Whereas a direct cast would throw InvalidCast. I think this is a useful distinction, because it's therefore a condensed version of something like: (Foo is Bar ? (cast (Bar) Foo) : (cast (Bar) null)); Cheers, --AJG. |
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | AJG wrote: > What do you guys think of the C# 'as' operator? I think it was inherited from VisualBasic. Could be wrong. > # (foo as Bar).someBarFunc(); > > You've got to admit it's a much cleaner syntax. Admittedly, it is. Kind of pretty, in fact. > Note: I believe in C# this is an Exception-safe operation, and it will return > null on failure. Whereas a direct cast would throw InvalidCast. I think this is > a useful distinction, because it's therefore a condensed version of something > like: > > (Foo is Bar ? (cast (Bar) Foo) : (cast (Bar) null)); In D the cast() operation currently returns null if it fails with objects. So it already behaves just like your 'as' expression. Maybe in D the 'as' expression (if it existed) would be the one to throw an exception on failure... so that doing: # Foo foo = new Foo; # Bar bar = foo as Bar; Would be semantically equivelant to: # Foo foo = new Foo; # Bar bar = cast(Bar) foo; # if (bar is null) # throw new InvalidCastException(Foo.classinfo.name, Bar.classinfo.name); Or something like that. -- Chris Sauls |
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | Needless to say, it has been suggested before, and it's still a good idea. L. |
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | "Chris Sauls" <ibisbasenji@gmail.com> wrote in message news:dafsqi$1k9a$1@digitaldaemon.com... > In D the cast() operation currently returns null if it fails with objects. So it already behaves just like your 'as' expression. Maybe in D the 'as' expression (if it existed) would be the one to throw an exception on failure... so that doing: Man, would _that_ confuse prospective D users coming from C# ;) cast() and as switched! I _do_ like the "as" syntax. |
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | AJG wrote:
> What do you guys think of the C# 'as' operator?
>
> It's essentially a more aesthetic way to make a cast, but if incorporated into
> D, it could be made different. For example:
>
> # void func(Foo foo) {
> # // You can do:
> # Bar bar = foo as Bar;
> # bar.someBarFunc();
> # # // Or even the highly effective:
> # (foo as Bar).someBarFunc();
> # }
>
> You've got to admit it's a much cleaner syntax.
>
> Note: I believe in C# this is an Exception-safe operation, and it will return
> null on failure. Whereas a direct cast would throw InvalidCast. I think this is
> a useful distinction, because it's therefore a condensed version of something
> like:
>
> (Foo is Bar ? (cast (Bar) Foo) : (cast (Bar) null));
>
> Cheers,
> --AJG.
>
>
It may look better, but it doesn't stick out as much as 'cast' does.
Also, when you do a search for 'as' you will get more than just instances of the keyword 'as'
|
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | In article <dagv0m$2fd9$1@digitaldaemon.com>, Jarrett Billingsley says... > >"Chris Sauls" <ibisbasenji@gmail.com> wrote in message news:dafsqi$1k9a$1@digitaldaemon.com... >> In D the cast() operation currently returns null if it fails with objects. So it already behaves just like your 'as' expression. Maybe in D the 'as' expression (if it existed) would be the one to throw an exception on failure... so that doing: > >Man, would _that_ confuse prospective D users coming from C# ;) cast() and as switched! > >I _do_ like the "as" syntax. > Well, I'm coming from C# (amongst others) and I don't feel confused :) It wasn't hard to switch to cast(). While I also like the "as" syntax, my proposition is to leave it as it is. There are simply more important things to be done (e.g. that whole const/immutable thing) ... Just my 2 c, Stefan |
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | AJG wrote: > What do you guys think of the C# 'as' operator? Definitely sweeter looking than 'cast', but not exactly an essential addition, since it can easily be duplicated with normal casting. I wouldn't mind it and probably would even use it, but I won't miss it if it isn't implemented. clayasaurus wrote: > Also, when you do a search for 'as' you will get more than just instances of the keyword 'as' Which is why you should search for 'as' surrounded by whitespace. The Perl-compatible regexp /\s+as\s+/ should do the trick. Plaintext searching can get a bit trickier (say, if somebody's decided to break the line right before the 'as', and then uses tabs for indentation), but in most cases ' as ' works. It's how I always search for keywords :-) |
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | Deewiant wrote:
> AJG wrote:
>
>>What do you guys think of the C# 'as' operator?
>
>
> Definitely sweeter looking than 'cast', but not exactly an essential
> addition, since it can easily be duplicated with normal casting. I
> wouldn't mind it and probably would even use it, but I won't miss it if
> it isn't implemented.
>
> clayasaurus wrote:
>
>>Also, when you do a search for 'as' you will get more than just
>>instances of the keyword 'as'
>
>
> Which is why you should search for 'as' surrounded by whitespace. The
> Perl-compatible regexp /\s+as\s+/ should do the trick. Plaintext
> searching can get a bit trickier (say, if somebody's decided to break
> the line right before the 'as', and then uses tabs for indentation), but
> in most cases ' as ' works.
>
> It's how I always search for keywords :-)
But it will still pick up...
void set(int) // set class bob as an int
:-P
whereas with cast you can search for 'cast('
|
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | clayasaurus wrote:
> Deewiant wrote:
>> clayasaurus wrote:
>>
>>> Also, when you do a search for 'as' you will get more than just instances of the keyword 'as'
>>
>>
>>
>> Which is why you should search for 'as' surrounded by whitespace. The Perl-compatible regexp /\s+as\s+/ should do the trick. Plaintext searching can get a bit trickier (say, if somebody's decided to break the line right before the 'as', and then uses tabs for indentation), but in most cases ' as ' works.
>>
>> It's how I always search for keywords :-)
>
>
> But it will still pick up...
>
> void set(int) // set class bob as an int
>
> :-P
>
> whereas with cast you can search for 'cast('
Which will still pick up...
foo = bar; // here I don't need to use cast(baz)bar because...
;-)
|
July 06, 2005 Re: [Suggestion] The 'As' Operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | Deewiant wrote:
> clayasaurus wrote:
>
>>Deewiant wrote:
>>
>>>clayasaurus wrote:
>>>
>>>
>>>>Also, when you do a search for 'as' you will get more than just
>>>>instances of the keyword 'as'
>>>
>>>
>>>
>>>Which is why you should search for 'as' surrounded by whitespace. The
>>>Perl-compatible regexp /\s+as\s+/ should do the trick. Plaintext
>>>searching can get a bit trickier (say, if somebody's decided to break
>>>the line right before the 'as', and then uses tabs for indentation), but
>>>in most cases ' as ' works.
>>>
>>>It's how I always search for keywords :-)
>>
>>
>>But it will still pick up...
>>
>>void set(int) // set class bob as an int
>>
>>:-P
>>
>>whereas with cast you can search for 'cast('
>
>
> Which will still pick up...
>
> foo = bar; // here I don't need to use cast(baz)bar because...
>
> ;-)
If you know what type you are looking for. It will take a while to search int = float, bar = foo, char = ubyte, etc. With cast, you just search for 'cast(' and collect them all.
IMO, cast() sticks out like a sore thumb, as it should. It's the reason why C style casting was depreciated...
int bob = (int)floatnum;
It is harder to find this type of cast in the code, I assume 'as' would be a little easier to find than (int), but cast() is uglier and easier to find.
Should cast's in D blend nicely with the code and be easy to program? I don't think we want to encourage casting, do we?
|
Copyright © 1999-2021 by the D Language Foundation