On 10/2/2017 10:32 AM, bartc wrote:
> On 02/10/2017 14:19, Rick C. Hodgin wrote:
>> On 10/2/2017 9:09 AM, bartc wrote:
>
>>>> void my_function(int a, int b, to max, total, int c)
>
>>>> // max = 3, total = 3
>
>>> I've read it, and can't understand it. And you have two versions of
>>> my_function(), but both apparently do the same thing.
>>
>> The first my_function() stores the incoming 3rd parameter to the
>> types indicated by max and total.
>
> Max and total are types (which types?) or variables?
In the original post they were identified as local variables
within the function:
void my_function(int a, int b, to max, total, int c)
{
int i = 5;
int max, total;
}
>> The second my_function() declares the incoming 3rd parameter as
>> two int variables, with the 3rd parameter being automatically
>> copied into both.
>
> So what's the difference here, or isn't there any?
One of them declares max and total on the function declaration
line, the other uses the variables declared internally as local
variables.
>>> What problem does this solve: how would the equivalent be done in C
>>> now, and how would this new feature transform that?
>>
>> In C today:
>>
>> my_function(1, 2, 3, 4);
>>
>> void my_function(int a, int b, int p3, int c)
>> {
>> int max = p3;
>> int total = p3;
>> int i = 5;
>> }
>
>> The p3 value is received as a single parameter, and it is manually
>> copied to max and total.
>
> OK, everyone can understand this.
>
>>> And in the above, how many parameters does my_function() actually
>>> take, is it 3, 4 or 5? Ie. how many values does the caller put on the
>>> stack before passing control, or is there some magic that happens at
>>> the call-site too?
>>
>> You pass in four parameters, but this parameter redirect ability kind
>> of reinterprets the parameters as incoming slots. Those slots then
>> align with their parameter definition in the target function, but can
>> also have logic applied by the compiler to automatically re-direct an
>> incoming parameter to multiple targets, or to multiple types, allowing
>> for the compiler to inject that code for you without multiple lines of
>> source code, or explicit casting (where required).
>
> Sorry, I can't parse that! But if all it does is copy a parameter to
> multiple values, then it is easy enough to do now.
It is. But, it requires lines of code to do it, and those can be
scattered about.
> Using one (IMO more useful) extension to avoid re-stating a parameter
> type, the above can be written like this:
>
> void my_function(int a, b, max, c) {
> int total = max;
> int i = 5;
>
> This assumes you don't need p3 as well (so three copies of the parameter
> are needed). If so then it's what you have in C apart from writing the
> params as 'int a, b, p3, c)'.
I don't see that saving as beneficial, and largely for the same
reason I don't see using tabs instead of spaces as beneficial.
If I use tabs, then I can make minor tweaks and adjustments to
my code and it will only occasionally move other things that are
aligned nearby. If I use spaces, I have to make adjustments to
aligned code every time I change the width of anything.
Similarly, in your example, if I suddenly wanted to convert one of
the parameters after int a to another type, I'd have to make more
than one change to the line. It also has me scanning left on each
parameter to see how it's defined.
With parameter re-directs, you can have an incoming parameter be
an int, and have it auto-redirected/copied into other types, like
a float or larger / smaller / unsigned integer type.
The biggest benefit I can see is assigning a pass-thru value to
a return variable (something CAlive allows, but C/C++ do not),
by name, so that the only code that will change that value is
something processed in the function.
>> It de-clutters code,
>
> I've not convinced that this would be very useful. It happens sometimes
> that you need to take a copy of an incoming parameter, leaving the
> original unchanged. But that can be done most easily, AND WITHOUT ADDING
> A NEW FEATURE, just by writing:
>
> int total = p3;
It has more utility than that. You could assign to a global variable,
for example, which then brings the reference to the global variable as
being tied explicitly to that function's input, for documentation
purposes. It has the ability to assign things before any local function
code is run. In C++ or CAlive, if it was a class that's being copied,
it will call the constructors before-hand, etc.
It won't have utility in most programming circumstances, but I can see
some where it will help with code clarity, documentation, and it helps
to re-define the input parameters to a function as something other than
mechanical things. They become logical things, and logical things can
be altered into other things.
We'll see how it goes. I appreciate your feedback, Bart.