-
Notifications
You must be signed in to change notification settings - Fork 53
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
Allow return type of external function to depend on typedesc parameter value #426
Comments
@sanjiva suggests also providing a way to default a typedesc param value to the contextually expected type |
The static typing of this can work because functions are covariant in their return types, but we need a restriction that the return type uses the referenced typedesc argument covariantly. For example, if some built-in type |
Consider: stream<Customer,sql:Error> stm = client->query(“SELECT ...”); where query is defined as function query(string q, typedesc rowType) returns stream<rowType, sql:Error>; In this case, we want to default the rowType parameter to Customer. So it’s not quite as simple as defaulting to the contextually expected type. Rather the contextually expected type gives the return type and from this return type we determine the default for the rowType. |
Hmm that's true .. slightly more complicated. Basically the compiler has to solve a small constraint satisfaction problem to derive the value for the rowType. |
We need a syntax. Semantically it's not really the same as defaulting (where the expression specifying the default value is evaluated in the context of the definition). Best I have thought of so far: function query(string q, typedesc rowType?) returns stream<rowType, sql:Error> = external; |
I was thinking of something like this: function query(string q, typedesc rowType = ?) returns stream<rowType, sql:Error> = external; It's using the defaulting syntax but by saying "?" its saying that its not like regular defaulting .. but rather computed by the compiler. |
Only one character difference! What I don't like about that syntax is that function query(string q, typedesc rowType = <>) returns stream<rowType, sql:Error> = external; The |
Not that I don't like it, but your syntax I'm totally +1 for the |
I agree: I much prefer the |
Not that this is only for external functions, because the static type rules of the language cannot enforce that a function creates a value that belongs to a type specified by the value of a variable of typedesc type. |
We should change |
I am including in this issue applying this to the return type of |
Because of case-insensitivity (#425), I think in the SQL connector we will need to pass the desired Ballerina type to the query method as as parameter of type typedesc, e.g.:
But then we have to specify the type as both a parameter and in the cast, e.g.
which is pretty obnoxious.
I think there is a similar situation with the HTTP client.
With generic functions and the parameter to typedesc, we could do something like:
In this view, you have a generic function that you can instantiate with a particular type parameter to create a function that you call. Another way to think of it is that you have one function, but the result type is dependent on a parameter.
So I wonder if we could do this:
The rowType typedesc parameter is referenced as a type descriptor in the return type. With this you would not need the cast when you call the query method.
Initially we could allow this only for external functions, so we don't have to come up with rules for what the function body of such a function would like.
We can use this to describe typedesc:constructFrom also:
The text was updated successfully, but these errors were encountered: