Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-29 Thread Xiangrong Fang

 No, there is no difference, because even if we would implement support for
 function based operators we would not implement any default ones or only
 provide them with an additional unit. So you either need to implement them
 yourself or use an additional unit anyway.


I mean, with the proposed way, you can just use all predefined functions,
such as Min or Max, also, it is much easier to just define a function than
write a class helper. But anyway, these are only little tricks...
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Mark Morgan Lloyd

Benito van der Zander wrote:

Hi,
quite often you need to change a value relatively to another value.
For example:

  array1[array2[i]] := array1[array2[i]] + 42;

Luckily this can be written as

  array1[array2[i]] += 42;

Which is nice.

However, sometimes you do not need addition, but the minimum.
For example:

   array1[array2[i]] := min(array1[array2[i]], 42);

Now, you need to repeat all the array indices.

Which is very ugly.

So there should be an alternative syntax, similar to += :
I.e.:

   array1[array2[i]] min= 42;


Declare a custom function and mark it inline for efficiency. That also 
allows you to document it properly, have different implementations 
depending on the parameter types and so on.


There really are limits to the extent that the underlying Pascal should 
be mangled further. Your time would be better spent trying to persuade 
the core developers to tolerate extra operators like ⌊ for floor(), and 
then working out how to implement them :-)


array1[array2[i]] := array1[array2[i]] ⌊ 42;
array1[array2[i]] ⌊= 42;

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Sven Barth

Am 28.03.2013 16:23, schrieb Benito van der Zander:

Hi,
quite often you need to change a value relatively to another value.
For example:

  array1[array2[i]] := array1[array2[i]] + 42;

Luckily this can be written as

  array1[array2[i]] += 42;

Which is nice.

However, sometimes you do not need addition, but the minimum.
For example:

   array1[array2[i]] := min(array1[array2[i]], 42);

Now, you need to repeat all the array indices.

Which is very ugly.

So there should be an alternative syntax, similar to += :
I.e.:

   array1[array2[i]] min= 42;


More generally, if func is a 2-ary function, of type type(a) = 
type(b) = type(a), the syntax


a func= b

should become a := func(a, b)

(Or alternatively the syntax   a : func = b;  might be easier to parse)

There was already a discussion some time ago whether we should allow 
operators like or= and such as well and the result was simple: no. I 
consider this the same here.


You can achieve a similar effect through type helpers in 2.7.1 though:

=== code begin ===

program mintest;

{$mode objfpc}

uses
  Math;

type
  TLongIntHelper = type helper for LongInt
procedure Min(aValue: LongInt);
  end;

procedure TLongIntHelper.Min(aValue: LongInt);
begin
  Self := Math.Min(Self, aValue);
end;

var
  arr: array[0..20] of LongInt;
  i: LongInt;
begin
  for i := Low(arr) to High(arr) do
arr[i] := i;

  for i := Low(arr) to High(arr) do
Write(arr[i], ' ');
  Writeln;

  arr[15].Min(10); // arr[15] is passed as Self to Min

  for i := Low(arr) to High(arr) do
Write(arr[i], ' ');
  Writeln;
end.

=== code end ===

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Benito van der Zander


There was already a discussion some time ago whether we should allow 
operators like or= and such as well and the result was simple: no. I 
consider this the same here. 


But it would be more consistent with +=

You can achieve a similar effect through type helpers in 2.7.1 though: 

looks cool, but I only have 2.6.2...




type
  TLongIntHelper = type helper for LongInt
procedure Min(aValue: LongInt);
  end;

procedure TLongIntHelper.Min(aValue: LongInt);
begin
  Self := Math.Min(Self, aValue);
end; 


Perhaps that should be defined in the rtl


On 03/28/2013 05:47 PM, Sven Barth wrote:

Am 28.03.2013 16:23, schrieb Benito van der Zander:

Hi,
quite often you need to change a value relatively to another value.
For example:

  array1[array2[i]] := array1[array2[i]] + 42;

Luckily this can be written as

  array1[array2[i]] += 42;

Which is nice.

However, sometimes you do not need addition, but the minimum.
For example:

   array1[array2[i]] := min(array1[array2[i]], 42);

Now, you need to repeat all the array indices.

Which is very ugly.

So there should be an alternative syntax, similar to += :
I.e.:

   array1[array2[i]] min= 42;


More generally, if func is a 2-ary function, of type type(a) = 
type(b) = type(a), the syntax


a func= b

should become a := func(a, b)

(Or alternatively the syntax   a : func = b;  might be easier to parse)

There was already a discussion some time ago whether we should allow 
operators like or= and such as well and the result was simple: no. I 
consider this the same here.


You can achieve a similar effect through type helpers in 2.7.1 though:

=== code begin ===

program mintest;

{$mode objfpc}

uses
  Math;

type
  TLongIntHelper = type helper for LongInt
procedure Min(aValue: LongInt);
  end;

procedure TLongIntHelper.Min(aValue: LongInt);
begin
  Self := Math.Min(Self, aValue);
end;

var
  arr: array[0..20] of LongInt;
  i: LongInt;
begin
  for i := Low(arr) to High(arr) do
arr[i] := i;

  for i := Low(arr) to High(arr) do
Write(arr[i], ' ');
  Writeln;

  arr[15].Min(10); // arr[15] is passed as Self to Min

  for i := Low(arr) to High(arr) do
Write(arr[i], ' ');
  Writeln;
end.

=== code end ===

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Benito van der Zander



Declare a custom function and mark it inline for efficiency. That also 
allows you to document it properly, have different implementations 
depending on the parameter types and so on. 


that's overkill if it is only an internal datastructure


our time would be better spent trying to persuade the core developers 
to tolerate extra operators like ⌊ for floor(), and then working out 
how to implement them :-) 
Or allow unicode identifier, and than defining ⌊ for ⌊= would just be a 
special case of func=



On 03/28/2013 04:45 PM, Mark Morgan Lloyd wrote:

Benito van der Zander wrote:

Hi,
quite often you need to change a value relatively to another value.
For example:

  array1[array2[i]] := array1[array2[i]] + 42;

Luckily this can be written as

  array1[array2[i]] += 42;

Which is nice.

However, sometimes you do not need addition, but the minimum.
For example:

   array1[array2[i]] := min(array1[array2[i]], 42);

Now, you need to repeat all the array indices.

Which is very ugly.

So there should be an alternative syntax, similar to += :
I.e.:

   array1[array2[i]] min= 42;


Declare a custom function and mark it inline for efficiency. That also 
allows you to document it properly, have different implementations 
depending on the parameter types and so on.


There really are limits to the extent that the underlying Pascal 
should be mangled further. Your time would be better spent trying to 
persuade the core developers to tolerate extra operators like ⌊ for 
floor(), and then working out how to implement them :-)


array1[array2[i]] := array1[array2[i]] ⌊ 42;
array1[array2[i]] ⌊= 42;


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Jürgen Hestermann

Benito van der Zander wrote:

Luckily this can be written as
  array1[array2[i]] += 42;


I would use Pascal (not C) and write

inc(array1[array2[i]],42);

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Benito van der Zander


I would use Pascal (not C) and write

inc(array1[array2[i]],42);


well, that won't help with min

On 03/28/2013 06:14 PM, Jürgen Hestermann wrote:

Benito van der Zander wrote:

Luckily this can be written as
  array1[array2[i]] += 42;


I would use Pascal (not C) and write

inc(array1[array2[i]],42);

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Jürgen Hestermann

Benito van der Zander wrote:

array1[array2[i]] := min(array1[array2[i]], 42);
Now, you need to repeat all the array indices.
Which is very ugly.
So there should be an alternative syntax, similar to += :
I.e.:
   array1[array2[i]] min= 42;


Now *that* is ugly. It would take me quite a while to find out what happens 
here.
What is assigned to what and when?
Why making Pascal look like C instead of using C?
In the example case I would prefer the original or write

if array1[array2[i]]42 then
array1[array2[i]] := 42;

or if performance is realy an issue you I would use an intermediate pointer to 
the array type:

var p : ^arrayelementtype;

p := @array1[array2[i]];
p^ := min(p^,42);
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Benito van der Zander
Now *that* is ugly. It would take me quite a while to find out what 
happens here.


Perhaps :min= was better?


Why making Pascal look like C instead of using C?


C also does not support this :(

And it does not have arbitrary array index ranges



In the example case I would prefer the original or write 

In the real program I ended up with



if array1[array2[i]]42 then
array1[array2[i]] := 42; 


Then you have everything twice.

And if you have to change i to j, you might forget to change one


On 03/28/2013 06:26 PM, Jürgen Hestermann wrote:

Benito van der Zander wrote:

array1[array2[i]] := min(array1[array2[i]], 42);
Now, you need to repeat all the array indices.
Which is very ugly.
So there should be an alternative syntax, similar to += :
I.e.:
   array1[array2[i]] min= 42;


Now *that* is ugly. It would take me quite a while to find out what 
happens here.

What is assigned to what and when?
Why making Pascal look like C instead of using C?
In the example case I would prefer the original or write

if array1[array2[i]]42 then
array1[array2[i]] := 42;

or if performance is realy an issue you I would use an intermediate 
pointer to the array type:


var p : ^arrayelementtype;

p := @array1[array2[i]];
p^ := min(p^,42);
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Mark Morgan Lloyd

Sven Barth wrote:


type
  TLongIntHelper = type helper for LongInt
procedure Min(aValue: LongInt);
  end;

procedure TLongIntHelper.Min(aValue: LongInt);
begin
  Self := Math.Min(Self, aValue);
end;



  arr[15].Min(10); // arr[15] is passed as Self to Min


I thought there was something like that, but I couldn't put my finger on 
it :-)


--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Sven Barth
Am 28.03.2013 18:02 schrieb Benito van der Zander ben...@benibela.de:


 There was already a discussion some time ago whether we should allow
operators like or= and such as well and the result was simple: no. I
consider this the same here.


 But it would be more consistent with +=

Consistency does not necessarily make them desireable.



 You can achieve a similar effect through type helpers in 2.7.1 though:

 looks cool, but I only have 2.6.2...

Then you'll need to wait until 2.8.0 is released (and no, I don't have an
estimate). And even if we'd implement your proposal you'd need to wait for
the next major version...





 type
   TLongIntHelper = type helper for LongInt
 procedure Min(aValue: LongInt);
   end;

 procedure TLongIntHelper.Min(aValue: LongInt);
 begin
   Self := Math.Min(Self, aValue);
 end;


 Perhaps that should be defined in the rtl

Some helper could be defined in additional units, but the problem with them
is that only one per type can be active at a time (Delphi compatible). My
plan is to change this though, but for this I first need to define proper
search rules for this...

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Feature proposal: function-based assignment operators

2013-03-28 Thread Xiangrong Fang
2013/3/29 Sven Barth pascaldra...@googlemail.com

 There was already a discussion some time ago whether we should allow
 operators like or= and such as well and the result was simple: no. I
 consider this the same here.

 You can achieve a similar effect through type helpers in 2.7.1 though:


Using type helper here may result in apparently same effect. But in fact
they are very different.  The syntax OP proposed is a mechanism of the
language (compiler), i.e. support will be ubiquitous, but if you implement
it with type helper, then you have to write the helper (or use it) every
time you need such very tiny feature (if not just save typing), which
voids the whole idea of make the source code simpler.  Unless you put such
helper into the RTL, which will be similar to the OP's proposal, but not as
elegant.

My opinion is that I like this feature, but I don't mind if it is
implemented or not. That will indeed make the syntax more complex and a
little un-pascal-ish.

As to the C-style += operator, I have one question, will it makes code
slightly faster? because if you use Inc() or Dec() there will be a function
call?  or, the compiler will always try to inline Inc or Dec?
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal