need clarification: will typedef, C struct initialization, etc.

Leandro Lucarella llucax at gmail.com
Mon May 31 19:31:18 PDT 2010


Justin Spahr-Summers, el 31 de mayo a las 16:41 me escribiste:
> On Tue, 01 Jun 2010 06:05:50 +0800, Lionello Lunesu 
> <lio at lunesu.remove.com> wrote:
> > 
> > On 1-6-2010 5:38, Andrei Alexandrescu wrote:
> > > On 05/31/2010 03:54 PM, bearophile wrote:
> > >> Andrei Alexandrescu:
> > >>> typedef is gone.
> > >>
> > >> *mewls* I have shown here some examples of typedef usage, and I'll
> > >> keep posting few more in future. I'd like to pull it back from the
> > >> grave and keep it :-)
> > > 
> > > It's wasted time. typedef is gone.
> > > 
> > > Andrei
> > 
> > :((
> > 
> > I also miss typedef. I thought D had a great opportunity to fix it.
> > 
> > Take something like the Windows headers. It mostly consists of typedefs
> > for handles and whatnot. Without typedef you'd have to use alias and
> > type safety is out of the windows.
> > 
> > So what would be the way to translate those Windows headers? Create a
> > unique struct for each old typedef? With alias this, and a ctor? Well,
> > if that's the way to do it now, why not make typedef a shortcut for
> > exactly that!?
> 
> I'm a fan of typedef, personally, but the example you mentioned *would* 
> be solved using just an alias. 'alias' provides the same functionality 
> as C/C++'s 'typedef' and more.

I think he talks about this difference:

alias int a1;
alias int a2;
typedef int t1;
typedef int t2;

void fa(a2 a) {}
void ft(t2 t) {}

void main() {
	a1 a = 5;
	fa(a); // <--- compiles fine
	t1 t = 10;
	ft(t); // <--- error
}

function a.ft (t2) does not match parameter types (t1) types (t1)
cannot implicitly convert expression (t) of type t1 to t2


Alias does not provide type-safety.

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
There is no such thing as right or wrong
Only consequences


More information about the Digitalmars-d mailing list