Array as a list of parameters/it

From Free Pascal wiki
Jump to navigationJump to search

English (en) italiano (it)

Premesse

Questo è un tentativo di definire l'idea che sta dietro l'uso di un array come lista di valori. Questi valori possono essere aggiunti come una lista di parametri in open array/array di costanti, piuttosto che in una definizione type di variabili, dove il tipo è, per esempio, "array of string".

Proverò a descrivere i pro e i contro di questa caratteristica e a spiegare perchè la considero una caratteristica positiva e molto importante di FPC.

Questra descrizione evidenzierà dei problemi o la necessità di risolvere qualche aspetto, e proverà a spiegare come questa caratteristica possa essere d'aiuto, o dove si possano trovare problemi utilizzandola.

Spero che altri contribuiscano a questa definizione, per implementare questa caratteristica al meglio.

Perchè abbiamo bisogno di questa caratteristica?

Per rispondere a questa domanda, userò degli esempi che questa caratteristica può risolvere:

Esempio 1

  • Abbiamo un array di valori da una interrogazione SQL, e vogliamo usarlo, ad esempio per aggiungere un output usando la funzione "format". Per ottenere questo, dovremo creare una nostra funzione "format" che prenderà l'array e sostituirà la stringa con le nostre regole di formattazione, quindi possiamo andare in tutti i campi di un array.

Idee

  1. Questa fattura renderà la funzione format già esistente valida senza bisogno di riscriverla.
  2. Questa fattura darà un uso migliore delle data types che saranno usate. Invece di scrivere cicli, ed anche pensare a diversi modi di progettare l'output.
  3. Invece di riscrivere "ogni"tm in cui i programmatori devono usare array, mentre viene accettato solo open array/array of const,
  4. Le procedure write/writeln e read/readln sono pasado procedure create durante la compilazione dal compiler. Questa possibilità permette al compilatore di creare queste funzioni quando i programmatori dovrebbero crearsi da sè il loro System unit, or creare pesado procedure come write/writeln.
  5. Questa fattura ci darà il debug o semplicemente input e output, senza un programma scritto per farlo.

Problemi

  1. E' veramente necessario, o serve solo a maneggiare più pigramente arrays ?
  2. Cosa succede se dobbiamo usare records ?
  3. E se vogliamo usare un array come una variabile contenente diversi valori ?
  4. In what compiler modes should we add such feature ?
  5. What happen if developers wish to use only part of the array instead of the whole cells ?
  6. What happen when we wish to do the same to containers such as TList, or TStringList ?
  7. What happen if the array is array of Class, array of Record, or array of custom type ?
  8. Let's say that this feature exists, what happen to the following example:
     format(content, [ArrayWithFields, variableA..variableN]); ?
  9. What happen if we enable this option, and we place such array to a non open array/array of const ?

Esempio 2

  • Stiamo leggendo un file di testo e aggiungiamo il contenuto ad un array. Vogliamo portare tutte le possibilità di "c" in "Pascal" su tutte le linee.
    un modo può essere di correre sull'array con cicli innesati (il primo ciclo sarà ogni "cella", e il secondo cercherà "C" e chiamerà la procedura replaceAll). Questo è un modo molto inefficiente per risolvere il problema.
    Un secondo modo sarà di convertire questo "array di stringa" in una grande AnsiString, e chiamerà "replaceall" una volta sola, quando il vero ciclo sarà dentro "replaceall".

Ideas

  1. We can replace all of the occurrences without loosing the structure of such array, and without attempting to move from one format to another.
  2. Unlike most programming languages, we do not need to create different container such as "vectors" in Java in order to store array of string, and if we do not wish to use the Classes unit for TStrings/TList decanted to store such information (for example we can not or do not want to use classes and objects), we still can mimic such a solution.
  3. If we have a need to replace more occurrences we could make it with less time, and might be even with better optimization of the code.

Problems

  1. The array is still not one variable, and while it seems for the developers that only one loop happens, this does not means that the actual code is translated that way anyhow.
  2. Some (if not all) of the nested for loops can move into recursive functions, and that "might" increase speed.
  3. Memory maps such as mmap might give you better ways to do it without the need for such action.

Answers