[Gambas-user] Explictly sized array parameters in method signatures?

Tobias Boege taboege at ...626...
Wed Jan 28 13:19:01 CET 2015


On Wed, 28 Jan 2015, adamnt42 at ...626... wrote:
> Hmm, I've never tried this before.
> 
> 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.
> 

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

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list