Recent

Author Topic: FPC and... some feature request  (Read 10912 times)

MrShoor

  • New Member
  • *
  • Posts: 11
FPC and... some feature request
« on: October 01, 2015, 08:46:48 am »
Hi all.
I have more than 10 years working with Delphi, and just now (about one year) starts working with Lazarus+FPC.
I wondered this IDE. Wondered codetools. Lazarus/FPC team made great project.
But I also wondered, why it's still have some uncomfortable features (dynamic arrays and generics in general).

1. Dynamic arrays.
I want to add new one item at end of array.
Code: [Select]
  SetLength(MyArray, Length(MyArray)+1);
  MyArray[Length(MyArray)-1] := NewItem;
Rly? Most languages at world allow do this:
Code: [Select]
MyArray.Append(NewItem)
Ok. I have 2 different units (Unit1 and Unit2 for example). Both have type TBytes = array of Byte;
But types Unit1.TBytes and Unit2.TBytes not compatible! Damn. And not safe and fast way to resolve this problem. Using typecast not safe. Any future refactoring could broke code invisibly for programmer. Copying data - slow way, and not usable for sharing pointers case.

You want to sort array? Haha, not so fast, guy. I still not found any sort function for arrays at standard library.
Wiki have 5 implementation of sort algorithms: http://wiki.lazarus.freepascal.org/Category:Sort
Wiki!!! Not standard library! Seems guys who wrote this code confused with Lazarus Wiki.
Anyway you should copy-paste this code and hope thats work. Or you can wrote sort self, ha ha.
By the way most languages allow: MyArray.Sort()
Sure I have own implementation of sort. I think any Delphi programmer wrote own sort function. But I will very very very very glad to see:
Code: [Select]
  MyArray.Append(item)
  MyArray.Append(OtherArray)
  MyArray.Delete(index)
  MyArray.DeleteRange(fromIndex, toIndex)
  MyArray.IndexOf(item)
  MyArray.Sort(Comparator)
and compatibility same arrays declaration between units

2. Generics.
Hey. Who invented specialize keyword and strict typing before use? Why I can't simply write:
Code: [Select]
  var
    myarray: IArray<ItemType>;
Whyyyyyyyyyy? Guy who invented, answer plsss.
I really trying use generics... but my code awful. Just look at this:
Code: [Select]
    TMeshVertexFunc = specialize TMurmur2Hash<TMeshVertex>;
    IMeshVerticesHash = specialize IHashMap<TMeshVertex, Integer, TMeshVertexFunc>;
    TMeshVerticesHash = specialize THashMap<TMeshVertex, Integer, TMeshVertexFunc>;   
And this only for one single variable:
Code: [Select]
  mvHash: IMeshVerticesHash;Instead:
Code: [Select]
  mvHash: IMeshVerticesHash<TMeshVertex, Integer, TMurmur2Hash<TMeshVertex>>;Somebody, please. Remove this restriction from language.

///////////////////////////////////
Yes, new language (objfpc) have wonderful features in comparison with Delphi. Macroses, really good operators overloading, but alsow borrowed/kept uncomfortable features like above.
Anyway lot and lot thanks for developers who doing this compiler and ide.

And sorry for my english.

derek.john.evans

  • Guest
Re: FPC and... some feature request
« Reply #1 on: October 01, 2015, 09:07:08 am »
1. Dynamic arrays.

True. It would be nice, but, generics arn't quite stable enough for such a system (and I doubt it is a good idea to extend the language/compiler). I have a STL style generic library written, but, it requires FPC 3.1.1. It will be at least a year (maybe more) before that becomes standard.

2. Generics.

In Delphi mode, you dont need the generic/specialize commands.

RE: mvHash: IMeshVerticesHash<TMeshVertex, Integer, TMurmur2Hash<TMeshVertex>>;

Yep, FPC 3.1.1 handles that stuff. Its coming.

Just takes some time.
« Last Edit: October 01, 2015, 09:37:35 am by Geepster »

derek.john.evans

  • Guest
Re: FPC and... some feature request
« Reply #2 on: October 01, 2015, 09:13:57 am »
Here is a sneak preview of my SGL code. It wont be finished for 6-12 months. requires FPC 3.1.1

sgl_examples shows what can be done.
sgl_algorithm shows that alogrithms from STL can be ported to Pascal.

Im still unsure if this is the best way to go. From a speed/reliability POV, its still better to use bog standard TList's, and dynamic arrays.

But, its fun playing with a STL style system in Pascal.

GetMem

  • Hero Member
  • *****
  • Posts: 3648
Re: FPC and... some feature request
« Reply #3 on: October 01, 2015, 10:01:01 am »
Code: [Select]
@Geepster
Here is a sneak preview of my SGL code. It wont be finished for 6-12 months. requires FPC 3.1.1
Very nicely done! It looks promising. I have a question though: what is the obsession ascii 95?  :D Did you work as a C programmer before? Personally I find that ugly.

derek.john.evans

  • Guest
Re: FPC and... some feature request
« Reply #4 on: October 01, 2015, 10:18:23 am »
Very nicely done! It looks promising. I have a question though: what is the obsession ascii 95?  :D Did you work as a C programmer before? Personally I find that ugly.

ASCI95? Unsure what you mean. Hasn't everyone worked as a C coder before?

If you mean the code format style, um, a lot of it was just copied and pasted from various C++ sources. ie: http://www.cplusplus.com/ and https://www.sgi.com/tech/stl/

I guess I tried to keep it as close to the originals as possible. Personally, this project was just me exploring what was possible.

Im a explorer. Not a debator. So, after coding all of that (ie SGL)..... I think its better to just suck it up and use the standard TStringList/TObjectList.

Its pretty easy to code up a generic object for dynamic arrays of a given type.


MrShoor

  • New Member
  • *
  • Posts: 11
Re: FPC and... some feature request
« Reply #5 on: October 01, 2015, 10:21:15 am »
2. Generics.

In Delphi mode, you dont need the generic/specialize commands.

RE: mvHash: IMeshVerticesHash<TMeshVertex, Integer, TMurmur2Hash<TMeshVertex>>;

Yep, FPC 3.1.1 handles that stuff. Its coming.

Just takes some time.
Yeah. But I lose then nice operator overloading feature. And as far as I remember - this mode of generics have a lot limitations too. FPC can't compile even Generics.Collections.pas unit from Delphi. But I did'n check it at 3.1.1 yet, so may be it can compile now.

GetMem

  • Hero Member
  • *****
  • Posts: 3648
Re: FPC and... some feature request
« Reply #6 on: October 01, 2015, 10:33:13 am »
Quote
@Geepster
ASCI95? Unsure what you mean.
I mean underscore, all variables begin with: "_" ( _key,  _first, _items, etc.). This is very common among c programmers. I'm not saying is a bad thing I just don't like it, not even when I'm coding in c.


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9778
  • FPC developer.
Re: FPC and... some feature request
« Reply #7 on: October 01, 2015, 11:00:06 am »
In 3.x there are type helpers possible iirc.  And there is the tarray generic array.

And the chance that define before use gets overturned is near zero.  That is Pascal base anxiom. (and it really improves error generation, which should be recognizable if you are used to using generics/templates in C++)


Btw, my own generics attempts are at http://www.stack.nl/~marcov/genlight.pas a more Delphi approach with iterators etc.
« Last Edit: October 01, 2015, 11:53:30 am by marcov »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9778
  • FPC developer.
Re: FPC and... some feature request
« Reply #8 on: October 01, 2015, 11:54:42 am »
Quote
@Geepster
ASCI95? Unsure what you mean.
I mean underscore, all variables begin with: "_" ( _key,  _first, _items, etc.). This is very common among c programmers. I'm not saying is a bad thing I just don't like it, not even when I'm coding in c.

There are more such stylistic things in that codebase, like the unnecessary repeating of e.g. TYPE keywords.

Personally I hate the FPC generics syntax and the object usage.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8403
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FPC and... some feature request
« Reply #9 on: October 01, 2015, 12:06:44 pm »
1. Dynamic arrays.
I want to add new one item at end of array.
Code: [Select]
  SetLength(MyArray, Length(MyArray)+1);
  MyArray[Length(MyArray)-1] := NewItem;
Rly? Most languages at world allow do this:
Code: [Select]
MyArray.Append(NewItem)
Dynamic array is not the proper type for such an operation. Use T[[FPG][Object]]List or any other similar data structure classes. Now you might have the idea why many of those languages name the type list instead of array, though some still name it array.
Reference here: http://wiki.lazarus.freepascal.org/Data_Structures,_Containers,_Collections
Ok. I have 2 different units (Unit1 and Unit2 for example). Both have type TBytes = array of Byte;
But types Unit1.TBytes and Unit2.TBytes not compatible! Damn. And not safe and fast way to resolve this problem. Using typecast not safe. Any future refactoring could broke code invisibly for programmer. Copying data - slow way, and not usable for sharing pointers case.
No idea what you want to achieve with this.
You want to sort array? Haha, not so fast, guy. I still not found any sort function for arrays at standard library.
Wiki have 5 implementation of sort algorithms: http://wiki.lazarus.freepascal.org/Category:Sort
Wiki!!! Not standard library! Seems guys who wrote this code confused with Lazarus Wiki.
Anyway you should copy-paste this code and hope thats work. Or you can wrote sort self, ha ha.
By the way most languages allow: MyArray.Sort()
Sure I have own implementation of sort. I think any Delphi programmer wrote own sort function. But I will very very very very glad to see:
Code: [Select]
  MyArray.Append(item)
  MyArray.Append(OtherArray)
  MyArray.Delete(index)
  MyArray.DeleteRange(fromIndex, toIndex)
  MyArray.IndexOf(item)
  MyArray.Sort(Comparator)
and compatibility same arrays declaration between units
Back to above wiki link.
2. Generics.
Hey. Who invented specialize keyword and strict typing before use? Why I can't simply write:
Code: [Select]
  var
    myarray: IArray<ItemType>;
Whyyyyyyyyyy? Guy who invented, answer plsss.
I really trying use generics... but my code awful. Just look at this:
Code: [Select]
    TMeshVertexFunc = specialize TMurmur2Hash<TMeshVertex>;
    IMeshVerticesHash = specialize IHashMap<TMeshVertex, Integer, TMeshVertexFunc>;
    TMeshVerticesHash = specialize THashMap<TMeshVertex, Integer, TMeshVertexFunc>;   
And this only for one single variable:
Code: [Select]
  mvHash: IMeshVerticesHash;Instead:
Code: [Select]
  mvHash: IMeshVerticesHash<TMeshVertex, Integer, TMurmur2Hash<TMeshVertex>>;Somebody, please. Remove this restriction from language.
What awful? The latter code is the awful one. Now construct that object, is that beautiful? Typing <x,y,z> over and over again?

I can CLEARLY SEE you are using other languages' brain when coding Object Pascal. Either you start thinking in Object Pascal way or you will be annoyed by your own annoyance all the time.
« Last Edit: October 01, 2015, 12:08:28 pm by Leledumbo »

derek.john.evans

  • Guest
Re: FPC and... some feature request
« Reply #10 on: October 01, 2015, 12:39:38 pm »
Quote
@Geepster
ASCI95? Unsure what you mean.
I mean underscore, all variables begin with: "_" ( _key,  _first, _items, etc.). This is very common among c programmers. I'm not saying is a bad thing I just don't like it, not even when I'm coding in c.

There are more such stylistic things in that codebase, like the unnecessary repeating of e.g. TYPE keywords.

Personally I hate the FPC generics syntax and the object usage.

I agree. I had to code something to demo the "what if" or "what is possible".

Personally, I think the standard classes are better. But, you can see my point (hopefully). Im a big fan of standard Delphi style coding, but sometimes you just have to explore the arguments in order to expose the truth.

The code is there for anyone to use. I dont, and I wrote it!
« Last Edit: October 01, 2015, 12:44:20 pm by Geepster »

sam707

  • Guest
Re: FPC and... some feature request
« Reply #11 on: October 01, 2015, 02:20:17 pm »
another language limitation =

S set of DWORD

S := [1..8920,9110..10240] (or set of strings maybe, or set of database indices)

for i in S do begin ...
  computerecord(r [ i ]); { if set of dword or db indices }
  comparekeyword_tocmd[ i ]; { if set of strings used in an interpreter for example}
                  { actually im using 2 sets in an interpreter with 317 kwds }
    end; {... with set larger than 256 elems all would be great... }

we all have ideas (even looking simple but complex to implement) to extend $mode objfpc

problem is time to make those patches
« Last Edit: October 01, 2015, 02:42:57 pm by sam707 »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: FPC and... some feature request
« Reply #12 on: October 01, 2015, 03:02:20 pm »
Ok. I have 2 different units (Unit1 and Unit2 for example). Both have type TBytes = array of Byte;
But types Unit1.TBytes and Unit2.TBytes not compatible! Damn. And not safe and fast way to resolve this problem.
i must have misunderstood what you wrote  because:

Two different units, two different type declarations -> they should differ.

Create a 3rd unit
Code: [Select]
Unit Common;
interface
type
  TByteArray = Array of byte;
implementation
end.

And let the other two units 'inherit' its type from there:

Code: [Select]
Unit Unit1;
interface
uses Common;
Type
  TBytes = Common.TByteArray;
implementation
end.

Code: [Select]
Unit Unit2;
interface
uses Common;
Type
  TBytes = Common.TByteArray;
implementation
end.

and test

Code: [Select]
program test;
uses
  unit1, unit2;
Var
  var1: Unit1.TBytes; 
  var2: Unit2.TBytes;
begin
  // no idea what you wanted to test for them arrays to be compatible, but please do show what you had in mind
end.

But in your program you should really use the common declared type in unit common to keep things clear and be able to modify without the hassle of doing so for unit1 and unit2 as well.

Quote
But I will very very very very glad to see:
<snip>
advanced records can already do that for you. I believe other posters have shown a generics solution already.

(edit: corrected typo in code)
« Last Edit: October 01, 2015, 03:27:49 pm by molly »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2721
    • havefunsoft.com
Re: FPC and... some feature request
« Reply #13 on: October 01, 2015, 04:47:27 pm »
ugh.. generics! do it old-school way.
Code: [Select]
procedure AnyQuickSort(var Arr; idxL, idxH: Integer;
  Stride: Integer; CompareFunc: TCompareFunc; var SwapBuf);
var
  ls,hs : Integer;
  li,hi : Integer;
  mi    : Integer;
  ms    : Integer;
  pb    : PByteArray;
begin
  pb:=@Arr;
  li:=idxL;
  hi:=idxH;
  mi:=(li+hi) div 2;
  ls:=li*Stride;
  hs:=hi*Stride;
  ms:=mi*Stride;
  repeat
    while CompareFunc( pb[ls], pb[ms] ) < 0 do begin
      inc(ls, Stride);
      inc(li);
    end;
    while CompareFunc( pb[ms], pb[hs] ) < 0 do begin
      dec(hs, Stride);
      dec(hi);
    end;
    if ls <= hs then begin
      Move(pb[ls], SwapBuf, Stride);
      Move(pb[hs], pb[ls], Stride);
      Move(SwapBuf, pb[hs], Stride);
      inc(ls, Stride); inc(li);
      dec(hs, Stride); dec(hi);
    end;
  until ls>hs;
  if hi>idxL then AnyQuickSort(Arr, idxL, hi, Stride, CompareFunc, SwapBuf);
  if li<idxH then AnyQuickSort(Arr, li, idxH, Stride, CompareFunc, SwapBuf);
end;

procedure AnySort(var Arr; Count: Integer; Stride: Integer; CompareFunc: TCompareFunc);
var
  buf: array of byte;
begin
  SetLength(buf, Stride);
  AnyQuickSort(Arr, 0, Count-1, Stride, compareFunc, buf[0]);
end;

Example of use:
Code: [Select]
function CompareInt(const d1,d2): integer;
var
  i1 : integer absolute d1;
  i2 : integer absolute d2;
begin
  if i1=i2 then Result:=0
  else if i1<i2 then Result:=-1
  else Result:=1;
end;

procedure SortArrayInteger(var arr: array of Integer; count: Integer);
begin
  AnySort(arr, Count, sizeof(Integer), @CompareInt);
end;

MrShoor

  • New Member
  • *
  • Posts: 11
Re: FPC and... some feature request
« Reply #14 on: October 01, 2015, 08:30:26 pm »
Create a 3rd unit
Unit1 and Unit2 - 3d party libraries. You suggest change code of 3d party libraries?
advanced records can already do that for you.
You suggest wrap every type of array to record?
Instead:
Code: [Select]
  var some: array of TMyType;
  some.Append(newValue);
You suggest use that:
Code: [Select]
TAnotherOneNewType = record
  data: array of TMyType;
  procedure Append(const NewValue: TMyType);
end;

procedure TAnotherOneNewType.Append(const NewValue: TMyType);
begin
  SetLength(data, Length(data) + 1);
  data[Length(data)-1] := NewValue;
end;

var some: TAnotherOneNewType;
  some.Append(newValue);
?

 

TinyPortal © 2005-2018