Open poorna2152 opened 2 weeks ago
What are you suggesting it be modified to?
Both myself and @heshanpadmasiri had a discussion on this and what we thought was about was the following three options.
Define the forEach
and such langlib function to accept a union with non typeParam type as well
public isolated function forEach(xml<ItemType> x, @isolatedParam function(ItemType item) returns ()|function(Element|Comment|ProcessingInstruction|Text item) returns () func)
Change the definition of typeParams.
Define a new annotation for this usage.
Out of these I think the first one is the better option.
I'm still not sure I understand why this is invalid:
xml b = xml `<a/><b/>`;
// Based on the spec this shouldn't work because the `ItemType` is not an upper bound `xml`
b.forEach(function (xml e) {
});
Let's define
type X function(Element|Comment|ProcessingInstruction|Text item) returns ();
type Y function (xml item) returns ();
If we instantiate the type of forEach
based on the type of b
, it would expect an argument that is a subtype of X (since xml
is the same as xml<Element|Comment|ProcessingInstruction|Text>
). But we are providing it with a function of type Y and Y is a subtype of X (since functions are covariant in their parameter types and Element|Comment|ProcessingInstruction|Text
is a subtype of xml
).
Am I confused?
Here the function is passed a parameter of type ItemType
which is a typeParam.
@typeParam
type ItemType Element|Comment|ProcessingInstruction|Text;
When a parameter is marked as a typeParam it defines an upper bound for the type that can be passed to that. Here Element|Comment|ProcessingInstruction|Text
is not an upper bound for xml
. And also when a parameter is defined as a typeParam we do not check the covariant right.
Description: Based on the spec, the type defined by the
typeParam
is defined to be an upper bound on the type parameter.In the case of
xml
consider the followingforEach
function definition.Where the
ItemType
is defined as,The second parameter
func
in theforEach
function defines a function which accepts parameteritem
of typeItemType
.Consider following examples,
With the definition of typeParam and the forEach function in the spec second case is invalid. This is because the in the second case the ItemType is not an upper bound for
xml
. This worked so far in the compiler due to a bug. Shouldn't the definition in the spec be modified to support this case as well?Suggested Labels:
Code sample that shows issue:
Related Issues: