www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Shortcuts

reply Trevor Parscal <Trevor_member pathlink.com> writes:
I just wrote a new templated shortcut module for Fusion - and I thought I would
suggest inclusion in phobos - since it's so darn handy. Some might think this
isn't their style - but it can be useful in making things look nice and clear
IMHO.

The idea is that you can have "multi" argument properties like...

/////////////////////////////////
class FOO
{
int[] Range(int[] _values)
in
{
asert(_values.length >= 2);
}
body
{
this.start = _values[0];
this.end = _values[1];
return _values;
}
}
/////////////////////////////////

and than you can use the property like...

/////////////////////////////////

foo.Range = pair(1, 10);

/////////////////////////////////

There are 3 shortcuts. all deal with values of a single type.

pair has 2 arguments, and always returns a dynamic array with 2 elements.
trio has 3 arguments, and always returns a dynamic array with 3 elements.
list is variadic, and always returns a dynamic array with at least 1 element.

The code can be seen on the dsource svn...

http://svn.dsource.org/projects/terra/trunk/Fusion/Source/Fusion/Composite.d

The source is free as in beer - just let your buddies know who gave you the
beer!

Thanks,
Trevor Parscal
Feb 26 2006
next sibling parent James Dunne <james.jdunne gmail.com> writes:
Trevor Parscal wrote:
 I just wrote a new templated shortcut module for Fusion - and I thought I would
 suggest inclusion in phobos - since it's so darn handy. Some might think this
 isn't their style - but it can be useful in making things look nice and clear
 IMHO.
 
 The idea is that you can have "multi" argument properties like...
 
 /////////////////////////////////
 class FOO
 {
 int[] Range(int[] _values)
 in
 {
 asert(_values.length >= 2);
 }
 body
 {
 this.start = _values[0];
 this.end = _values[1];
 return _values;
 }
 }
 /////////////////////////////////
 
 and than you can use the property like...
 
 /////////////////////////////////
 
 foo.Range = pair(1, 10);
 
 /////////////////////////////////
 
 There are 3 shortcuts. all deal with values of a single type.
 
 pair has 2 arguments, and always returns a dynamic array with 2 elements.
 trio has 3 arguments, and always returns a dynamic array with 3 elements.
 list is variadic, and always returns a dynamic array with at least 1 element.
 
 The code can be seen on the dsource svn...
 
 http://svn.dsource.org/projects/terra/trunk/Fusion/Source/Fusion/Composite.d
 
 The source is free as in beer - just let your buddies know who gave you the
 beer!
 
 Thanks,
 Trevor Parscal

Hmm, I don't know. That removes a lot of typing information. Also, dynamic memory is allocated per each function call, which makes it look less appealing, not to mention non-performant. And finally, the list method is kinda scary - a silent failure case hidden away in your casting on pointers. What's to be gained here? -- Regards, James Dunne
Feb 26 2006
prev sibling parent reply Trevor Parscal <Trevor_member pathlink.com> writes:
In article <dtsl1i$1n4e$1 digitaldaemon.com>, Trevor Parscal says...
I just wrote a new templated shortcut module for Fusion - and I thought I would
suggest inclusion in phobos - since it's so darn handy. Some might think this
isn't their style - but it can be useful in making things look nice and clear
IMHO.

The idea is that you can have "multi" argument properties like...

/////////////////////////////////
class FOO
{
int[] Range(int[] _values)
in
{
asert(_values.length >= 2);
}
body
{
this.start = _values[0];
this.end = _values[1];
return _values;
}
}
/////////////////////////////////

and than you can use the property like...

/////////////////////////////////

foo.Range = pair(1, 10);

/////////////////////////////////

There are 3 shortcuts. all deal with values of a single type.

pair has 2 arguments, and always returns a dynamic array with 2 elements.
trio has 3 arguments, and always returns a dynamic array with 3 elements.
list is variadic, and always returns a dynamic array with at least 1 element.

The code can be seen on the dsource svn...

http://svn.dsource.org/projects/terra/trunk/Fusion/Source/Fusion/Composite.d

The source is free as in beer - just let your buddies know who gave you the
beer!

Thanks,
Trevor Parscal

OR perphaps not - I was messing with it allot more, using different types, and allot of casting is needed to support all types.. so - I guess this will never really work on a generic level. Sorry for posting before testing extensively... I get excited sometimes.. :) Thanks, Trevor Parscal
Feb 26 2006
parent James Dunne <james.jdunne gmail.com> writes:
Trevor Parscal wrote:
 In article <dtsl1i$1n4e$1 digitaldaemon.com>, Trevor Parscal says...
 
I just wrote a new templated shortcut module for Fusion - and I thought I would
suggest inclusion in phobos - since it's so darn handy. Some might think this
isn't their style - but it can be useful in making things look nice and clear
IMHO.

The idea is that you can have "multi" argument properties like...

/////////////////////////////////
class FOO
{
int[] Range(int[] _values)
in
{
asert(_values.length >= 2);
}
body
{
this.start = _values[0];
this.end = _values[1];
return _values;
}
}
/////////////////////////////////

and than you can use the property like...

/////////////////////////////////

foo.Range = pair(1, 10);

/////////////////////////////////

There are 3 shortcuts. all deal with values of a single type.

pair has 2 arguments, and always returns a dynamic array with 2 elements.
trio has 3 arguments, and always returns a dynamic array with 3 elements.
list is variadic, and always returns a dynamic array with at least 1 element.

The code can be seen on the dsource svn...

http://svn.dsource.org/projects/terra/trunk/Fusion/Source/Fusion/Composite.d

The source is free as in beer - just let your buddies know who gave you the
beer!

Thanks,
Trevor Parscal

OR perphaps not - I was messing with it allot more, using different types, and allot of casting is needed to support all types.. so - I guess this will never really work on a generic level. Sorry for posting before testing extensively... I get excited sometimes.. :) Thanks, Trevor Parscal

Hehe. Understood. -- Regards, James Dunne
Feb 26 2006