[SR-Dev] script parsing: string switch support

Henning Westerholt henning.westerholt at 1und1.de
Tue Feb 24 11:06:39 CET 2009


On Friday 20 February 2009, Andrei Pelinescu-Onciul wrote:
> [..]
> > > I just checked in kamailio, we don't throw an error if one mix strings
> > > and ints in a switch case. We also check for a correct type of an
> > > integer in a mixed expression, e.g. if its a valid int value. I don't
> > > think that we convert them implicitly, as many pseudo-variables hold
> > > internally both a string and a integer value, so mixed expression will
> > > work just as intended in
> >
> > Sorry, but that's just broken (mixed expressions).
>
> I don't really understand this argument. Leaving performance and
> optimizations aside, do you really think is better to guess what the
> user might have wanted in ambiguous cases, rather then through an error
> and point him to explain more clearly what he wants (e.g. use  match()
> instead of switch() for strings, or use typed variables)?

I think that its important to not make our config script too much like a "real 
programming language", with types and cast and this stuff. There is are 
reasons that script languages like python or perl are now widely used instead 
of e.g. C, even if their dynamic typed nature moves some error checks into 
run time. The benefits (higher expressiveness that leads to more compact 
code, fewer bugs because of automatic memory management etc..) outplay the 
drawbacks, IMHO.

> With the general approach advocated by some people here, a small error
> when writing the script (e.g. forgetting some "") will be visible only at
> runtime (and depending where that error is it could take weeks until
> that branch of the config is reached and the error is triggered).
> I think this is much worse then having to add a little more "details" to
> the script (which BTW wouldn't make it any more difficult then any
> common programming language).

Well, many people that uses our script language are not programmers. And even 
experienced crypto gurus seems to have difficulties with the complexity that 
common languages bring with it [1]. ;-)

> I'd rather have to declare even variable types (e.g. int $a), rather
> then debugging strange bugs in a script, because I used a var that
> happens to get a string value at runtime.
>
> In brief I much prefer adding to the script if this helps check its
> correctness at sr -cf time, rather then trying to be excessively friendly
> and leaving a lot of potential script bugs pass-through.
> The ability to be able to tell that the script would behave correctly
> (as his writer wanted it to) and eliminate ambiguous cases is more
> important than trying to keep a very reduced script and guessing what
> the writer really wanted.

I've of course nothing general against eleminating abiguous cases, like in the 
switch statement. If the user mix strings and int values in the cases, we 
really should report an error.

Cheers,

Henning

[1] http://blog.fortify.com/blog/fortify/2009/02/20/SHA-3-Round-1



More information about the sr-dev mailing list