Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 31, 2005 Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Remember me? I'm the guy that didn't like the syntax "cast(type)variable". Sorry to bring this up again... I recently came across some code while browsing, something like this: if (obj as SomeTypeName) .... I don't remember what language it was, but I liked that construction. The keyword "as" would fit nicely in D, like "in", "is", "isnot": it's practically english. It shouldn't replace cast(type) but maybe exist next to it, like === for "is" and !== for "isnot" exist next to eachother. So... Who else likes "as" for casting? Lionello (Uhm.. "isnot" doesn't seem to work though; what's the status of it? Will it be added? Patent problems?) |
March 31, 2005 Re: Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu wrote: > if (obj as SomeTypeName) .... > > I don't remember what language it was, but I liked that construction. The keyword "as" would fit nicely in D, like "in", "is", "isnot": it's practically english. I like it, better than the reinterpret_cast<SomeTypeName> "alternative". > It shouldn't replace cast(type) but maybe exist next to it, like === for "is" and !== for "isnot" exist next to eachother. > > So... Who else likes "as" for casting? Next to the regular one, sounds like a neat idea. Pretty minor, but... > (Uhm.. "isnot" doesn't seem to work though; what's the status of it? Will it be added? Patent problems?) There probably will never be a "isnot" keyword. I think due to ugliness? It would be much better if Walter could just say that, once and for all. --anders |
March 31, 2005 Re: Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu wrote: > Remember me? I'm the guy that didn't like the syntax "cast(type)variable". Sorry to bring this up again... > > I recently came across some code while browsing, something like this: > > if (obj as SomeTypeName) .... > > I don't remember what language it was, but I liked that construction. The keyword "as" would fit nicely in D, like "in", "is", "isnot": it's practically english. Object Pascal offers the "as" operator, but it is almost never used in the context like "if (obj as typeName) ...", because an exception will be raised if obj is not an instance of typeName or its derived types. > > It shouldn't replace cast(type) but maybe exist next to it, like === for "is" and !== for "isnot" exist next to eachother. > > So... Who else likes "as" for casting? > > Lionello > > (Uhm.. "isnot" doesn't seem to work though; what's the status of it? Will it be added? Patent problems?) > > |
March 31, 2005 Re: Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to zwang | zwang wrote:
> Object Pascal offers the "as" operator, but it is almost never used
> in the context like "if (obj as typeName) ...", because an exception will
> be raised if obj is not an instance of typeName or its derived types.
That's the same as how () casts work in Java, with ClassCastException.
In D, however, such a "cast()" will silently return null instead -
which makes constructs such as the "if ( )" above still work...
(it's rather similar to how the "in" works for the dynamic arrays,
it returns a null if it's not present - which works as boolean too)
Of course, it's only pretty if the language treats pointers as booleans.
--anders
|
March 31, 2005 Re: Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to zwang | "object as Class" should not behave different from cast(Class)object. Anyway, you'll get the AccessViolationException or what's it called when you actually try to use the null pointer. Furthermore you can do: assert( object as Class ); Has a nice ring to it, doesn't it? L. |
March 31, 2005 Re: Keyword | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | In article <d2gpru$rmu$1@digitaldaemon.com>, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says... > >Lionello Lunesu wrote: >> (Uhm.. "isnot" doesn't seem to work though; what's the status of it? Will it be added? Patent problems?) > >There probably will never be a "isnot" keyword. I think due to ugliness? My personal problem with "isnot" is that it looks like a horribly misnamed Apple product: iSnot. $0.02 - EricAnderton at yahoo.com |
March 31, 2005 Re: Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu wrote:
> if (obj as SomeTypeName) ....
>
> I don't remember what language it was, but I liked that construction.
Might've been Visual Basic. I know they have the "as Type" formula for variables, so it might have other uses as well. Having never really gotten far with Visual Basic, I couldn't say. I know XBasic (the language VB is literally a rip-off of) uses a colon for such things. Or did some years ago anyhow, haven't touched it in about... seven years.
-- Chris Sauls
|
March 31, 2005 Re: Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | > So... Who else likes "as" for casting?
I like it a lot. Very elegant.
-Craig
|
March 31, 2005 Re: Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | "Lionello Lunesu" <lio@lunesu.removethis.com> wrote in message news:d2gp84$r5u$1@digitaldaemon.com... > So... Who else likes "as" for casting? I suppose it looks a bit nicer, but I have two problems with it: one, I don't really find anything wrong with cast(type), and two, it would be quite a change to have to write casts "backwards." For example, a double cast, which looks like.. writefln("%x",cast(uint)cast(void*)A); would be writefln("%x",((A as void*) as uint)); Which looks, to me, even messier and more nonsensical. Something I always thought would be a nice alternative. In BASIC languages, there are functions such as "str()" and "int()" to cast values (though they're not used that much as most BASICs are weakly typed). Why not just make it possible to use types as.. functions? writefln("%x",uint(void*(A))); Then I remembered, this would probably clash with a _bunch_ of existing syntaxes (syntaces?). |
March 31, 2005 Re: Keyword "as" for casting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Personally, I quite like the "object as type" syntax. However - I don't think that D should have two completely different ways to do the same thing. So while I like it, I think that it shouldn't be used because we already have a construct that does that job. Now, if you could somehow get cast(type)object deprecated, and "object as type" made the One True Way of doing casting, then I would agree :) Brad |
Copyright © 1999-2021 by the D Language Foundation