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 29869 - Concise syntax for inline functions
Summary: Concise syntax for inline functions
Status: NEW
Alias: None
Product: XPath / XQuery / XSLT
Classification: Unclassified
Component: Requirements for Future Versions (show other bugs)
Version: Candidate Recommendation
Hardware: PC All
: 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-09-26 06:31 UTC by Michael Kay
Modified: 2016-12-07 18:35 UTC (History)
1 user (show)

See Also:


Attachments

Description Michael Kay 2016-09-26 06:31:13 UTC
Inline functions in XQuery are verbose. Consider

fn:sort($employees, function($emp as element(employee)) { $emp/@salary })

Programming in a purely function style becomes a lot easier with a compact syntax for writing inline functions. Compare:

XQuery (1)
function($x as xs:integer, $y as xs:integer) { $x + $y }

XQuery (2)
function($x, $y) {$x + $y}

Javascript 6
(x, y) => x+y

Java 8
(int x, int y) -> x+y

Scala (1)
(x: Int, y: Int) => x + y

Scala (2)
_ + _

Haskell
\ x y -> x + y

Python
lambda x, y: x + y


A couple of suggestions:

(a) Many use cases for simple inline functions take a single item as argument, and we could exploit the existing use of "." for the context item for such cases. For example use

\{. + 1} as shorthand for
function($x as item()){$x/(. + 1)}

allowing constructs such as

fn:sort($employees, \{@salary})

(b) For functions without declared types, we could implicitly declare the arguments as $1, $2, etc:

\{$1 + $2}

allowing

fn:sort($employees, \{$1/@salary})

Or we could combine the two ideas with "." being a synonym for ($1 treat as item()), thus

fn:sort($employees, \{@salary})

I'm not wedded to the backslash. Alternatives to \{$1+2} would be fn{$1+$2} or {|$1+$2|} or even bare {$1+$2}.
Comment 1 Benito van der Zander 2016-12-07 18:35:10 UTC
\{$1 + $2} looks awesome. Although you cannot set a type for $123. But types on anonymous functions  are only causing a mess with function coercion anyways





{|$1+$2|} is very bad. It is the JSONiq object merge syntax.  And the bare {$1+$2} would be confused with JSONiq object creation.