Autore Topic: Parametri esplicitamente dimensionati nelle funzioni  (Letto 245 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.683
  • Ne mors quidem nos iunget
    • Mostra profilo
Parametri esplicitamente dimensionati nelle funzioni
« il: 28 Gennaio 2015, 11:00:32 »
Vi riporto questa discussione:


" Suppose a function GetCurrent(DateRange as Date[]),  now intuitively DateRange is an array of 2 dates. No-one would be silly enough to pass an array with more than two elements would they?  My cynical nature says yes they could/would.

I could ignore the array length and just use DateRange[0] and DateRange[1] as the limits but that may produce confusing outcomes, or an error if DateRange contains only one element.

I would like to define the function as GetCurrent(DateRange as Date[2]) but this is apparently a syntax error.

Obviously, in this trivialised example, I could check the bounds of the passed array in the function body, I just expected that the runtime would  check an explicitly sized array parameter. The real function is (of course) much more complex. The function requires an array of Dates[3][2] and another 5 parameters, some of which are optional.

Any thoughts?

Bruce
"


" This may be over-simplified, but couldn't you define a DateRange structure? Or
would that not provide the constraints you need?

Lee
"


" It's actually a struct of structs (3x2)
Good thinking 99, I'll give that a go.

B
"


" That's how I do it in the Graph class. A vertex is identified via a name
String and an edge is a String[2]. I expect the user to provide a String[]
with at least 2 elements of which indices 2, 3, ... are ignored. If they
don't follow these rules, they can go jump in the lake -- or wherever it
is where runtime errors dwell.

The thing is, as you implied, that there is (likely) no error recovery
strategy which is guaranteed to be natural for everyone. My personal
practice in these cases is to let the interpreter raise the appropriate
error. Less defined behaviour in a component's interface makes it easier
to be backwards-compatible :-)

(The otherwise very good suggestion of using a Struct/Class was not
applicable to the graph case, IIRC.)

Regards,
Tobi
"
« Ultima modifica: 28 Gennaio 2015, 16:01:24 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »