Variable Declaration in Query Prolog

Going back to last month's discussion on the www-ql list, I would
like to suggest we need something like the following:

QueryProlog ::= (ParameterDecl | ...)* ...

ParameterDecl ::= 'define' 'parameter' [DataType] Variable [':=' Expr] ';'

This declares the named Variable to have "global" scope.
The specified Expr is only a default value, which may be
overriden in some application-specific matter.  Expr is
not evaluated if the parameter is specified externally.
The optional DataType is required if we want to support
static typing.

This construct fulfills two needs:
(1) A way to specify "global" variables whose scope encompasses
any function definition.
(2) A way to declare that variables are "imported" from
the external environment, in some application-specify way.
For example, one might want to specify arguments to 'document'
in this manner.

Why can't we just say that undeclared variables get their
values in the application environment?  This is a bad idea, for at
least these two reasons:
(1) If we allow implicitly declared global variables then we
cannot catch typos at compile-time.
(2) We cannot statically type implicitly declared global variables.

Can we wait to add this for a future version of XQuery?  No,
this is a feature that almost everyone will need (note that
XSLT does have it), and asking vendors to provide non-standard
extensions for such a critical feature would guarantee lots of
non-portable programs.

A couple of issues:

Should the scope of the declared Variable be restricted, for example
only to expressions *after* the ParameterDecl?  This avoids
circularity problems.  However, it isn't difficult to detect
circularity errors, and I think it would be more consistent to
allow ParameterDecl in arbitrary order (like Java static fields).

The syntax is problematic, becasuse we need a terminator after
the Expr.  If we had ';' as a general statement terminator or
separator, or made newlines significant there wouldn't be a problem.
I can think of other solutions:
'define' 'parameter' [DataType] Variable ['default' '(' Expr ')']
But what is best needs to be decided in the context of a consistent
QueryProlog syntax, and I don't know if that has stabilized yet.
-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/per/

Received on Monday, 4 March 2002 19:24:49 UTC