Jump to page: 1 2
Thread overview
[dmd-internals] Type-checked arrays!
Jul 05, 2011
Michel Fortin
Jul 05, 2011
mrmocool at gmx.de
Jul 05, 2011
Michel Fortin
Jul 05, 2011
Walter Bright
Jul 05, 2011
Michel Fortin
Jul 06, 2011
Walter Bright
Jul 06, 2011
Michel Fortin
Jul 06, 2011
Walter Bright
Jul 06, 2011
Jacob Carlborg
Jul 06, 2011
Michel Fortin
Jul 06, 2011
Jacob Carlborg
July 05, 2011
I've made a new branch of the DMD compiler to replace the currently type-unsafe Array type with arrays that can be type-checked by the compiler. It's not ready for a pull request yet, but I'd like to hear some comments.

Take a look at the changes: <https://github.com/michelf/dmd/compare/master...type-checked-arrays>

The basic goal is that you no longer have to cast to the right type when getting something out of an array (except for some exceptional cases). In arraytypes.h you define an array of the type you need using the ArrayOf macro like this:

	struct Expression;

	typedef ArrayOf(Expression) Expressions;

And voil?! you have a type-safe array-of-Expression type. Instead of accessing the 'data' member directly, you now access 'tdata()' (for typed-data) which gets you a properly typed pointer:

	Expression *e = array->tdata()[0];
	array->tdata()[0] = e;

I know it's ugly, but unfortunately C++ doesn't have properties. I'm open to suggestions.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



July 06, 2011
> 	Expression *e = array->tdata()[0];
> 	array->tdata()[0] = e;
>
> I know it's ugly, but unfortunately C++ doesn't have properties. I'm open to suggestions.

Is there any sane reason why operator[] can't be used?
July 05, 2011
I've always thought it should be done with a template:

    typedef ArrayOf<Expression> Expressions;

On 7/5/2011 3:04 PM, Michel Fortin wrote:
> I've made a new branch of the DMD compiler to replace the currently type-unsafe Array type with arrays that can be type-checked by the compiler. It's not ready for a pull request yet, but I'd like to hear some comments.
>
> Take a look at the changes: <https://github.com/michelf/dmd/compare/master...type-checked-arrays>
>
> The basic goal is that you no longer have to cast to the right type when getting something out of an array (except for some exceptional cases). In arraytypes.h you define an array of the type you need using the ArrayOf macro like this:
>
> 	struct Expression;
>
> 	typedef ArrayOf(Expression) Expressions;
>
> And voil?! you have a type-safe array-of-Expression type. Instead of accessing the 'data' member directly, you now access 'tdata()' (for typed-data) which gets you a properly typed pointer:
>
> 	Expression *e = array->tdata()[0];
> 	array->tdata()[0] = e;
>
> I know it's ugly, but unfortunately C++ doesn't have properties. I'm open to suggestions.
>
July 05, 2011
Le 2011-07-05 ? 18:52, Walter Bright a ?crit :

> I've always thought it should be done with a template:
> 
>   typedef ArrayOf<Expression> Expressions;

Well, now that I know I can change it to a template.

When I asked a mid-June on this list, I did not get an answer. I decided to go take the most conservative route and not use any C++ feature not already in use elsewhere in the code.

If you say it's okay to use templates then I'll make the change, it's no big deal. I agree it'd be a little cleaner that way, although with typedefs it won't change things much.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



July 05, 2011
Le 2011-07-05 ? 18:49, mrmocool at gmx.de a ?crit :

>> 	Expression *e = array->tdata()[0];
>> 	array->tdata()[0] = e;
>> 
>> I know it's ugly, but unfortunately C++ doesn't have properties. I'm open to suggestions.
> 
> Is there any sane reason why operator[] can't be used?

It makes refactoring simpler because there's an easily recognizable text pattern "tdata()". Once the current refactoring is done it'll be easy to change it to some other syntax, but currently my focus is on adding type-safety.

And I don't want to start using a C++ feature that isn't used anywhere else in the code base (not without Walter's approval first). Operator overloading isn't used anywhere inside DMD, and operator[] implies returning a '&' reference, another C++ feature not used in the code base (that I know of).

Also, most arrays are actually pointers to an array and I'm not sure how much the [] operator makes things clearer for those:

	Expression *e = (*array)[0];
	(*array)[0] = e;

:-/

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



July 05, 2011

On 7/5/2011 4:46 PM, Michel Fortin wrote:
>
> Well, now that I know I can change it to a template.
>
> When I asked a mid-June on this list, I did not get an answer. I decided to go take the most conservative route and not use any C++ feature not already in use elsewhere in the code.
>
> If you say it's okay to use templates then I'll make the change, it's no big deal. I agree it'd be a little cleaner that way, although with typedefs it won't change things much.
>

I originally did not use templates because g++ at the time did a terrible job with them. This has gotten much better now.
July 06, 2011
It's possible to emulate properties in C++ with some help of operator overloading: http://en.wikipedia.org/wiki/Property_%28programming%29#C.2B.2B

/Jacob Carlborg?

On 06 Jul, 2011,at 12:04 AM, Michel Fortin <michel.fortin at michelf.com> wrote:

> I've made a new branch of the DMD compiler to replace the currently type-unsafe Array type with arrays that can be type-checked by the compiler. It's not ready for a pull request yet, but I'd like to hear some comments.
>
> Take a look at the changes: <https://github.com/michelf/dmd/compare/master...type-checked-arrays>
>
> The basic goal is that you no longer have to cast to the right type when getting something out of an array (except for some exceptional cases). In arraytypes.h you define an array of the type you need using the ArrayOf macro like this:
>
> struct Expression;
>
> typedef ArrayOf(Expression) Expressions;
>
> And voil?! you have a type-safe array-of-Expression type. Instead of accessing the 'data' member directly, you now access 'tdata()' (for typed-data) which gets you a properly typed pointer:
>
> Expression *e = array->tdata()[0];
> array->tdata()[0] = e;
>
> I know it's ugly, but unfortunately C++ doesn't have properties. I'm open to suggestions.
>
> -- 
> Michel Fortin
> michel.fortin at michelf.com
> http://michelf.com/
>
>
>
> _______________________________________________
> dmd-internals mailing list
> dmd-internals at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-internals
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/dmd-internals/attachments/20110706/f869afb4/attachment-0001.html>
July 06, 2011
Le 2011-07-05 ? 20:15, Walter Bright a ?crit :

> On 7/5/2011 4:46 PM, Michel Fortin wrote:
>> 
>> Well, now that I know I can change it to a template.
>> 
>> When I asked a mid-June on this list, I did not get an answer. I decided to go take the most conservative route and not use any C++ feature not already in use elsewhere in the code.
>> 
>> If you say it's okay to use templates then I'll make the change, it's no big deal. I agree it'd be a little cleaner that way, although with typedefs it won't change things much.
> 
> I originally did not use templates because g++ at the time did a terrible job with them. This has gotten much better now.

That's useful to know.

Done, now using a template:

	typedef Array<struct Expression> Expressions;

<https://github.com/michelf/dmd/compare/master...type-checked-arrays>

Ok, so now I'd like to know how long can I expect it might take before integrating in mainline? It's important because this patch is very costly to maintain, as almost every commit I merge into from mainline causes conflicts I must resolve manually.

Also, it's been tested only on OS X. It's not easy for me to work on other platforms, so I guess I'd need someone else to try to compile it on Linux and Windows and fix the type-checking errors for the platform specific parts. Anyone wants to help?

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



July 06, 2011
Le 2011-07-06 ? 2:28, Jacob Carlborg a ?crit :

> It's possible to emulate properties in C++ with some help of operator overloading: http://en.wikipedia.org/wiki/Property_%28programming%29#C.2B.2B

Doesn't work in this case because the property needs access to a variable of the base class. I realize I could add a pointer to it in the property struct, but that'd increase the array size by one word, and I don't want that.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



July 06, 2011

On 7/6/2011 4:09 AM, Michel Fortin wrote:
> Le 2011-07-05 ? 20:15, Walter Bright a ?crit :
>
>> On 7/5/2011 4:46 PM, Michel Fortin wrote:
>>> Well, now that I know I can change it to a template.
>>>
>>> When I asked a mid-June on this list, I did not get an answer. I decided to go take the most conservative route and not use any C++ feature not already in use elsewhere in the code.
>>>
>>> If you say it's okay to use templates then I'll make the change, it's no big deal. I agree it'd be a little cleaner that way, although with typedefs it won't change things much.
>> I originally did not use templates because g++ at the time did a terrible job with them. This has gotten much better now.
> That's useful to know.
>
> Done, now using a template:
>
> 	typedef Array<struct Expression>  Expressions;
>
> <https://github.com/michelf/dmd/compare/master...type-checked-arrays>
>
> Ok, so now I'd like to know how long can I expect it might take before integrating in mainline? It's important because this patch is very costly to maintain, as almost every commit I merge into from mainline causes conflicts I must resolve manually.
>
> Also, it's been tested only on OS X. It's not easy for me to work on other platforms, so I guess I'd need someone else to try to compile it on Linux and Windows and fix the type-checking errors for the platform specific parts. Anyone wants to help?
>

It'll have to wait until after this release is done. Also, please do it as a pull request!
« First   ‹ Prev
1 2