This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 29422 - Object-oriented programming lite
Summary: Object-oriented programming lite
Status: NEW
Alias: None
Product: XPath / XQuery / XSLT
Classification: Unclassified
Component: Requirements for Future Versions (show other bugs)
Version: Candidate Recommendation
Hardware: PC Linux
: P2 normal
Target Milestone: ---
Assignee: Jim Melton
QA Contact: Mailing list for public feedback on specs from XSL and XML Query WGs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-06 15:32 UTC by Benito van der Zander
Modified: 2016-02-06 15:32 UTC (History)
0 users

See Also:


Attachments

Description Benito van der Zander 2016-02-06 15:32:10 UTC
If you want to work with an 3.1 array/map, it seems you always have to prefix the functions with array:. array:tail, array:head, which is way too verbose.

The solution in other languages is object oriented programmnig, where you have an object, and the methods are bound to it.
Scala shows how to combine functional programming with objects, but it really complicated.
Fortunately there is a simpler solution.

Since all functions are in a namespace, we just need to map/bind the namespace to the types.

For example with a grammar rule like:

"declare" "type" "function" "namespace"  URILiteral "for" SequenceType

Then the user writes

declare type function namespace "http://www.w3.org/2005/xpath-functions/array" for array(*);
declare type function namespace "http://www.w3.org/2005/xpath-functions/map" for map(*);

and the functions are bound to their types.


Now what does it mean if the functions are bound to a type?

For this we need a symbol to access the bound functions. E.g. “::“ the C++ class member operator (-> or . would be nicer, but conflict with the allowed names. Pascal's “^.” would work, too)

Then, similarly to the => operator.
For a sequence $s with clear type the expression $s::f($j) becomes Q{bound namespace}f($s, $j)

With this the user can write:

[1,2,3]::size() to get 3

or 

let $a as array(*) := [1,2,3] return $a::tail()::size()


It is not array or map specific.

You can do as well

declare type function namespace "http://www.w3.org/2005/xpath-functions/math" for xs:double;

and then write 2e0::pow(10)::sqrt() instead of math:pow(2,10)=>math:sqrt()


Or if you define your own types in a schema, you can bind functions to those types.
E.g. you can define a type myxs:traffic-light, which is either "red", "green" or "yellow" 
(perhaps also "yellow red" for European lights).
Then you can have a function whatever:next-phase($light as myxs:traffic-light) as myxs:traffic-light
and write let $light as myxs:traffic-light := ... return $light::next-phase() to get to the next phase.


This should not depend on static typing, since that is an optional feature,
so you can only use for expressions that have clear type, i.e. whose type can be decided trivially.

These expressions are (at least):
- variables or function parameters declared with a type, e.g. $foo as xs:integer
- treat and cast, e.g. ... treat as xs:integer
- function calls of functions declared with a return type, e.g. substring(...)
- literals, e.g. 4.5
- constructors, e.g. [1,2,3]