-
Notifications
You must be signed in to change notification settings - Fork 55
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
Clarification regarding Query Expression construct types #1252
Comments
We need to change
to explicitly disallow mapping type, so that it is consistent with
The reason why map is special here is that what you get when you iterate over a map is the values, but what you need to construct is |
Fixed by 5e76414 |
Can I know the reason why we should restrict Totally agree with restricting maps
|
The reason is that the semantics of constructing streams is significantly different from constructing other types. I took the view that the difference was significant enough that it should be marked by syntax. |
Thinking about this some more, the fundamental difference with stream is that clauses are evaluated lazily; this implies that typing is different for So I think it is worth considering whether we should change the spec to match the implementation here. |
If we make import ballerina/io;
public function main() {
stream<int> intStrm = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].toStream();
var evenStrm = stream from int i in intStrm
where isEven(i)
select i;
io:println("Do some tasks eagerly");
from int i in evenStrm
// isEven()'s io:println() will be called here
do {
io:println(i);
};
}
function isEven(int i) returns boolean {
io:println("isEven called");
return i % 2 == 0;
} But that restriction causes scenarios like below to throw a CE. Hence, I prefer avoiding the inferring restriction public function dummyTest() {
stream<int> intStrm = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].toStream();
from int i in (from int i in intStrm
where isEven(i)
select i)
do {
print(i);
};
} |
I agree that when the stream is temporary (like in your scenario) requiring the |
I agree; inferring the type does indeed make it easier for developers to write code. If the type is already specified on the LHS, repeating it on the RHS seems redundant. Therefore, if we can safely infer the type, I'm all for omitting the keyword However, in cases like Chiran's second example, having the keyword |
We will change the spec to match the implementation. |
Description:
Regarding the spec description related to query expression:
Ref: https://ballerina.io/spec/lang/master/#section_6.35
Let me phrase my confusion as follows:
Can a map value be created without
map
keyword?The spec specifies below points seperately
A query-expr that constructs a mapping **must start with the map keyword**.
Confusion whether the below statement is applicable for
maps
since the following comes just under thetable
explanationOtherwise, the applicable contextually expected type determines the basic type of the value constructed. If there is no contextually expected type, then the basic type of the value constructed is the basic type of expression following in; it is a compile-time error if the static type of this expression is not a subtype of one of the basic types that a query expression can construct.
Atm, the implementation supports constructing any basic type out of
list
,map
,table
,string
,xml
,stream
when there's no contextually expected type looking at the basic type of expression followingin
.Suggested Labels:
Code sample that shows issue:
Related Issues:
The text was updated successfully, but these errors were encountered: