Re: Function naming: Problems and proposed solution

> 
> But this is ugly and may break existing code that was supplying e.g. a
> sequence of arrays & sometimes only one array in the sequence.
> 
> So, we could add fn:without() designed to work that way, and deprecate
> remove(), maybe?
> 

If we allow unqualified function names to be used for multiple namespaces (like fn, array, and map), then we could certainly try to solve the conflicts like put() and remove() by renaming some of the functions, but this doesn't really solve the underlying problem.

Basically, unless we can find some way of binding functions based on the type of the arguments, we're going to end up with qualified names of some kind, and whether it's map:remove(), map.remove(), or remove-from-map(), we can't just call it remove() if there are other functions using that name. Using an artful synonym doesn't help the user recognise what the function does from its name (which is how we started this thread, with Dimitre observing that the two names "replace" and "replace-with" give you no hints as to how the functions differ.).

Which actually suggests another way forward, namely keyword-based calls:

sort(//employee, by: function($e){$e/salary})

where "by" is the name of the third argument, somehow causing this to be interpreted as a call on sort#3 with () as the value of the second argument. This would then allow the approach of providing replace-with as replace#5, invoked say as replace($str, $regex, apply: upper-case#1).

and similarly calls on map:remove and fn:remove could be resolved by use of different keywords.

But the details to make this work are likely to be very tricky, and it's a potential source of a lot more complexity.

(A simpler baby step, while I think about it, is to allow sort(//employee, , function($e){$e/salary}) - that is, leaving out an argument is the same as supplying an empty sequence. We made this change for the content of curly braces, so it's not a big deal.)

Michael Kay
Saxonica

Received on Saturday, 28 November 2020 23:52:26 UTC