Re: [fpc-pascal] cthreads

2014-07-27 Thread Mark Morgan Lloyd

Sven Barth wrote:

On 26.07.2014 19:50, leledumbo wrote:

Because then all apps are linked to it, also the ones that don't need

threads. Same for clocale and cwstrings.

If the widestring manager could be made by ourselves, is it possible for
thread manager as well?


Principiall yes, but the problem here would be external code that the 
program links to. E.g. Wine did something like this some time ago 
(before they switched to pthreads) and needed to simulate some 
structures so that libc switches to multithreaded mode... So if we have 
Pascal only code (like the compiler) this would work without big 
problems (if someone implements it of course ;) ), but if you have 3rd 
party code not written in FPC then problems might arise...


On the other hand, if somebody's linking in "alien" code then he should 
make himself aware of aware of the prerequisites, particularly since the 
threads are more likely to be in the main program (i.e. stuff that he's 
written) than in the library he's pulling in.


Going back to something less labour intensive, would it be possible to 
have s directive which has the effect that if a procedure isn't 
eliminated by smartlinking then the linker should warn if a specific 
library hasn't been linked?


--
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/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] SetLength procedure

2014-07-27 Thread Steve Gatenby
Could anybody enlighten me on how to make the 'SetLength' procedure into 
an equivalent function


I have many instances of
  SetLength(xxx, Length(xxx)+1);
  Num := High(xxx);

I would like to have Num :=  SetLength(xxx, Length(xxx)+1);
 or even something like Num := IncLength(xxx, 1);

These are on varying types of array, so not really feasible to create a 
function for each one.

Hoping it is possible within pascal itself :)

Thanks - SteveG
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] SetLength procedure

2014-07-27 Thread Jürgen Hestermann


Am 2014-07-27 12:10, schrieb Steve Gatenby:
> Could anybody enlighten me on how to make the 'SetLength' procedure into an 
equivalent function
> I have many instances of
>   SetLength(xxx, Length(xxx)+1);
>   Num := High(xxx);


What is the problem with this code?
Why force these 2 commands into a function?
Just to save a little bit of typing?
When *reading* this code it is much
clearer what it does as when reading

Num := IncLength(xxx, 1);


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


Re: [fpc-pascal] SetLength procedure

2014-07-27 Thread Steve Gatenby

It is only a personal preference, not a serious problem.

I would find code in the form of
  ArrayLen := IncLength(ArrayName, 1);

to be much more readable then

   SetLength(ArrayName, Length(ArrayName)+1);
   ArrayLen:= High(ArrayName);

I was just hoping somebody may know how its done.

Thanks - SteveG

On 27/07/14 20:09, Jürgen Hestermann wrote:


Am 2014-07-27 12:10, schrieb Steve Gatenby:
> Could anybody enlighten me on how to make the 'SetLength' procedure 
into an equivalent function

> I have many instances of
>   SetLength(xxx, Length(xxx)+1);
>   Num := High(xxx);


What is the problem with this code?
Why force these 2 commands into a function?
Just to save a little bit of typing?
When *reading* this code it is much
clearer what it does as when reading

Num := IncLength(xxx, 1);


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


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


Re: [fpc-pascal] SetLength procedure

2014-07-27 Thread leledumbo
> It is only a personal preference, not a serious problem. 
>
> I would find code in the form of 
>ArrayLen := IncLength(ArrayName, 1); 
> 
> to be much more readable then 
> 
> SetLength(ArrayName, Length(ArrayName)+1); 
> ArrayLen:= High(ArrayName); 
> 
> I was just hoping somebody may know how its done. 

Without support for generic function, currently you have to use generic
class method:

type
  generic TDynArrayUtil = class
  public
class function SetLength(var a: T; const n: Integer): Integer;
  end;

class function TDynArrayUtil.SetLength(var a: T; const n: Integer): Integer;
begin
  System.SetLength(a,Length(a) + n);
  Result := High(a);
end;

and use it like:

var
  a: TIntegerDynArray;
...
maxn := specialize TDynArrayUtil.SetLength(a,1);

note that you have to instantiate with the correct type.



--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/SetLength-procedure-tp5719807p5719810.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] SetLength procedure

2014-07-27 Thread Steve Gatenby

Thanks for that - I will give it a try :)


On 27/07/14 21:21, leledumbo wrote:

It is only a personal preference, not a serious problem.

I would find code in the form of
ArrayLen := IncLength(ArrayName, 1);

to be much more readable then

 SetLength(ArrayName, Length(ArrayName)+1);
 ArrayLen:= High(ArrayName);

I was just hoping somebody may know how its done.

Without support for generic function, currently you have to use generic
class method:

type
   generic TDynArrayUtil = class
   public
 class function SetLength(var a: T; const n: Integer): Integer;
   end;

class function TDynArrayUtil.SetLength(var a: T; const n: Integer): Integer;
begin
   System.SetLength(a,Length(a) + n);
   Result := High(a);
end;

and use it like:

var
   a: TIntegerDynArray;
...
maxn := specialize TDynArrayUtil.SetLength(a,1);

note that you have to instantiate with the correct type.



--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/SetLength-procedure-tp5719807p5719810.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


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


Re: [fpc-pascal] cthreads

2014-07-27 Thread Marco van de Voort
In our previous episode, leledumbo said:
> > Because then all apps are linked to it, also the ones that don't need 
> threads. Same for clocale and cwstrings. 
> 
> If the widestring manager could be made by ourselves, is it possible for
> thread manager as well?

You could make an own threadmanager in theory. I have some doubts about the
usability and practicality though. If it ever emerges I think it will remain
for specific cases, not a general replacement.

The difference with the unicode manager is that you per definition need OS
support for the general threadmanager case.  With unicode you have the
choice to go via the OS (and be light on bulky tables in every binary) or
implement it yourself based on the liberally licensed Unicode consortium's
tables.

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


Re: [fpc-pascal] cthreads

2014-07-27 Thread Marco van de Voort
In our previous episode, Mark Morgan Lloyd said:
> Going back to something less labour intensive, would it be possible to 
> have s directive which has the effect that if a procedure isn't 
> eliminated by smartlinking then the linker should warn if a specific 
> library hasn't been linked?

ELF binaries generally don't require to associate a symbol with a library
name to link, like on Windows.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] cthreads

2014-07-27 Thread Sven Barth

On 27.07.2014 11:39, Mark Morgan Lloyd wrote:

Sven Barth wrote:

On 26.07.2014 19:50, leledumbo wrote:

Because then all apps are linked to it, also the ones that don't need

threads. Same for clocale and cwstrings.

If the widestring manager could be made by ourselves, is it possible for
thread manager as well?


Principiall yes, but the problem here would be external code that the
program links to. E.g. Wine did something like this some time ago
(before they switched to pthreads) and needed to simulate some
structures so that libc switches to multithreaded mode... So if we
have Pascal only code (like the compiler) this would work without big
problems (if someone implements it of course ;) ), but if you have 3rd
party code not written in FPC then problems might arise...


On the other hand, if somebody's linking in "alien" code then he should
make himself aware of aware of the prerequisites, particularly since the
threads are more likely to be in the main program (i.e. stuff that he's
written) than in the library he's pulling in.


That's not true. E.g. the Qt libraries happily create threads for 
various background stuffs.


Regards,
Sven

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


Re: [fpc-pascal] SetLength procedure

2014-07-27 Thread Jürgen Hestermann


Am 2014-07-27 12:51, schrieb Steve Gatenby:

I would find code in the form of
  ArrayLen := IncLength(ArrayName, 1);
to be much more readable then
   SetLength(ArrayName, Length(ArrayName)+1);
   ArrayLen:= High(ArrayName);



Why? It is not clear which index ArrayLen will receive from IncLength.
You need to look up (or remember) what exactly this function does
while in the two-line version it is all clear and unambiguous.
Especially, if you not only add 1 element but more as in

  ArrayLen := IncLength(ArrayName,2);

   SetLength(ArrayName, Length(ArrayName)+2);
   ArrayLen:= High(ArrayName);

In the first version, which value will ArrayLen be set to?
High(ArrayName) or High(ArrayName)-1?
This is very clear in the second version.

IMO it is not worth investing time into writing a function
that obscures the code. If you look at it months later
you may no longer know what IncLength does exactly,
not to talk about if someone else has/wants to read
your code.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] cthreads

2014-07-27 Thread Henry Vermaak
On Sun, Jul 27, 2014 at 02:39:58PM +0200, Sven Barth wrote:
> On 27.07.2014 11:39, Mark Morgan Lloyd wrote:
> >Sven Barth wrote:
> >>On 26.07.2014 19:50, leledumbo wrote:
> Because then all apps are linked to it, also the ones that don't need
> >>>threads. Same for clocale and cwstrings.
> >>>
> >>>If the widestring manager could be made by ourselves, is it possible for
> >>>thread manager as well?
> >>
> >>Principiall yes, but the problem here would be external code that the
> >>program links to. E.g. Wine did something like this some time ago
> >>(before they switched to pthreads) and needed to simulate some
> >>structures so that libc switches to multithreaded mode... So if we
> >>have Pascal only code (like the compiler) this would work without big
> >>problems (if someone implements it of course ;) ), but if you have 3rd
> >>party code not written in FPC then problems might arise...
> >
> >On the other hand, if somebody's linking in "alien" code then he should
> >make himself aware of aware of the prerequisites, particularly since the
> >threads are more likely to be in the main program (i.e. stuff that he's
> >written) than in the library he's pulling in.
> 
> That's not true. E.g. the Qt libraries happily create threads for various
> background stuffs.

Ha, I had a library that started a thread and it took me ages to figure
out why my pascal program was crashing on callbacks from external
threads.  I had to start a dummy thread right at the start of the
program, even though I included cthreads.  Not a good experience all in
all.

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


Re: [fpc-pascal] SetLength procedure

2014-07-27 Thread Andreas Berger
I very seldom comment on this, or any, forum unless I have a good answer 
and no one else seem to have one. This case is different. I have noticed 
quit often here that instead of answering the question people try to 
reason why in the world the author of the question would even want to do 
this. Would it not be better to tell him how to do it or at the most say 
"I don't know". I know there are times when you want to prevent someone 
from making a serious mistake, but this is not the case here.
Sometimes the question is placed by someone new to programming and it is 
important to learn from your own work, mistakes and all. Often the 
question also comes from people migrating from, or knowledgeable with, 
another language where a similar functionality exits. This type of 
argument will only discourage them away from our beloved Pascal.


Nur mein Senf.

On Sun 27/07/2014 11:18, Jürgen Hestermann wrote:


Am 2014-07-27 12:51, schrieb Steve Gatenby:

I would find code in the form of
  ArrayLen := IncLength(ArrayName, 1);
to be much more readable then
   SetLength(ArrayName, Length(ArrayName)+1);
   ArrayLen:= High(ArrayName);



Why? It is not clear which index ArrayLen will receive from IncLength.
You need to look up (or remember) what exactly this function does
while in the two-line version it is all clear and unambiguous.
Especially, if you not only add 1 element but more as in

  ArrayLen := IncLength(ArrayName,2);

   SetLength(ArrayName, Length(ArrayName)+2);
   ArrayLen:= High(ArrayName);

In the first version, which value will ArrayLen be set to?
High(ArrayName) or High(ArrayName)-1?
This is very clear in the second version.

IMO it is not worth investing time into writing a function
that obscures the code. If you look at it months later
you may no longer know what IncLength does exactly,
not to talk about if someone else has/wants to read
your code.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



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


Re: [fpc-pascal] SetLength procedure

2014-07-27 Thread Jürgen Hestermann


Am 2014-07-27 19:28, schrieb Andreas Berger:

I have noticed quit often here that instead of answering the question people try to 
reason why in the world the author of the question would even want to do this. Would it 
not be better to tell him how to do it or at the most say "I don't know". I 
know there are times when you want to prevent someone from making a serious mistake, but 
this is not the case here.


I know what you mean but in this case I find it worth mentioning that the 2 
line code *is* easier to read.
And for me it would be wasting time to somehow find a solution for his problem 
instead of simply
using the 2 line code and use the saved time for real problems.
The 2 line code is definitely more in the spirit of Pascal than
obscuring what happens with a (not so clear) function.

But of course, everybody can do what he wants.
It was just meant in the sense of an advice.

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


Re: [fpc-pascal] SetLength procedure

2014-07-27 Thread Saunders, Rich

On 2014-07-27 13:28, Andreas Berger wrote:

I have noticed quit often here that instead of answering the question
people try to reason why in the world the author of the question would
even want to do this.


I agree that this is an annoying pattern on this list.

I would suggest everyone at least wait until someone tries to respond 
with an actual answer to the question before diverting discussion in 
this way.


--

Cheers!
Rich Saunders
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] cthreads

2014-07-27 Thread Sven Barth

On 27.07.2014 17:01, Henry Vermaak wrote:

On Sun, Jul 27, 2014 at 02:39:58PM +0200, Sven Barth wrote:

On 27.07.2014 11:39, Mark Morgan Lloyd wrote:

Sven Barth wrote:

On 26.07.2014 19:50, leledumbo wrote:

Because then all apps are linked to it, also the ones that don't need

threads. Same for clocale and cwstrings.

If the widestring manager could be made by ourselves, is it possible for
thread manager as well?


Principiall yes, but the problem here would be external code that the
program links to. E.g. Wine did something like this some time ago
(before they switched to pthreads) and needed to simulate some
structures so that libc switches to multithreaded mode... So if we
have Pascal only code (like the compiler) this would work without big
problems (if someone implements it of course ;) ), but if you have 3rd
party code not written in FPC then problems might arise...


On the other hand, if somebody's linking in "alien" code then he should
make himself aware of aware of the prerequisites, particularly since the
threads are more likely to be in the main program (i.e. stuff that he's
written) than in the library he's pulling in.


That's not true. E.g. the Qt libraries happily create threads for various
background stuffs.


Ha, I had a library that started a thread and it took me ages to figure
out why my pascal program was crashing on callbacks from external
threads.  I had to start a dummy thread right at the start of the
program, even though I included cthreads.  Not a good experience all in
all.


Yes, the FPC RTL is normally running in singlethreaded mode and at least 
one FPC thread (besides the main one) needs to be started to switch it 
in multithreaded mode. I thought that we had some improvements there at 
least in 2.7.1, but I'd need to check that again.


Regards,
Sven

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