Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose Type Aliases #18

Open
adamretter opened this issue Nov 30, 2018 · 4 comments
Open

Propose Type Aliases #18

adamretter opened this issue Nov 30, 2018 · 4 comments

Comments

@adamretter
Copy link
Member

adamretter commented Nov 30, 2018

Often I find that I want to have a name alias for a complex type definition. This would be particularly helpful in a number of scenarios:

  1. When defining an API, it would be nice to have a more descriptive name for the users of the API. I believe that this can increase code readability.

  2. Less typing, some type definitions are quite complex, a simple alias could reduce the amount of copy-paste semantics.

Suppose I have a function that takes an argument which is rather complex:

declare function local:my-function($a as (function() as map(xs:string, map(xs:string, (function(xs:integer) as xs:float+)*)))+)

It would be easier/nicer if I could have something like the following:

declare type alias local:lazy-math-functions
    as (function() as map(xs:string, map(xs:string, (function(xs:integer) as xs:float+)*)))+;

declare function local:my-function($a as local:lazy-math-functions) {
...
};

declare function local:my-other-function($a as local:lazy-math-functions, $b as xs:int+) {
...
};

Aliases are likely only syntactic sugar, they are not types themselves. The query processor should always report errors based on the real types, of course if the processor wants to keep the name of the type alias around for better error reporting, then it is free to do so.

We could also consider the vsisibility of type aliases of a module, perhaps we want to allow %public and %private like annotations.

@michaelhkay
Copy link
Member

michaelhkay commented Nov 30, 2018 via email

@michaelhkay
Copy link
Member

michaelhkay commented Nov 30, 2018 via email

@adamretter
Copy link
Member Author

@michaelhkay Ah Ha. Yes almost exactly like what Saxon has for type aliases :-)

Not too sure about types by restriction, we are effectively then attaching a validation function to type values. I need to give it some thought and consider it in the context of things like Refined Types - https://github.com/fthomas/refined#related-projects

@adamretter
Copy link
Member Author

I would also like to propose function aliases:

declare function alias xdmp:log($msg, $level) := util:log($level, $log)

This is useful where you have a function whose context is important, another example would be fn:error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants