Here's a patch that fixes the quirks around Type.fits() when nullable generic types are involved. (I do a lot of reflection and this keeps biting me in the proverbial.)
This fixes the bugs in Java and JS as mentioned in:
`http://fantom.org/forum/topic/2256`
`http://fantom.org/forum/topic/2453`
and brings the two runtimes into alignment.
diff -r fd89993adbdd src/sys/java/fan/sys/FuncType.java
--- a/src/sys/java/fan/sys/FuncType.java Wed Sep 23 17:17:28 2015 -0400
+++ b/src/sys/java/fan/sys/FuncType.java Fri Sep 25 01:03:00 2015 +0100
@@ -83,6 +83,10 @@
public boolean is(Type type)
{
if (this == type) return true;
+ if (type instanceof NullableType)
+ {
+ type = ((NullableType) type).root;
+ }
if (type instanceof FuncType)
{
FuncType t = (FuncType)type;
diff -r fd89993adbdd src/sys/java/fan/sys/ListType.java
--- a/src/sys/java/fan/sys/ListType.java Wed Sep 23 17:17:28 2015 -0400
+++ b/src/sys/java/fan/sys/ListType.java Fri Sep 25 01:03:00 2015 +0100
@@ -57,6 +57,10 @@
public boolean is(Type type)
{
+ if (type instanceof NullableType)
+ {
+ type = ((NullableType) type).root;
+ }
if (type instanceof ListType)
{
ListType t = (ListType)type;
diff -r fd89993adbdd src/sys/java/fan/sys/MapType.java
--- a/src/sys/java/fan/sys/MapType.java Wed Sep 23 17:17:28 2015 -0400
+++ b/src/sys/java/fan/sys/MapType.java Fri Sep 25 01:03:00 2015 +0100
@@ -62,6 +62,10 @@
public boolean is(Type type)
{
+ if (type instanceof NullableType)
+ {
+ type = ((NullableType) type).root;
+ }
if (type instanceof MapType)
{
MapType t = (MapType)type;
diff -r fd89993adbdd src/sys/js/fan/Type.js
--- a/src/sys/js/fan/Type.js Wed Sep 23 17:17:28 2015 -0400
+++ b/src/sys/js/fan/Type.js Fri Sep 25 01:03:00 2015 +0100
@@ -759,9 +759,12 @@
fan.sys.ListType.prototype.is = function(that)
{
+ if (that instanceof fan.sys.NullableType)
+ that = that.m_root;
+
if (that instanceof fan.sys.ListType)
{
- if (that.v.qname() == "sys::Obj") return true;
+ if (this.v.qname() == "sys::Obj") return true;
return this.v.is(that.v);
}
if (that instanceof fan.sys.Type)
@@ -848,11 +851,13 @@
fan.sys.MapType.prototype.is = function(that)
{
- if (that.isNullable()) that = that.m_root;
+ if (that instanceof fan.sys.NullableType)
+ that = that.m_root;
if (that instanceof fan.sys.MapType)
{
- return this.k.is(that.k) && this.v.is(that.v);
+ return (this.k.qname() == "sys::Obj" || this.k.is(that.k)) &&
+ (this.v.qname() == "sys::Obj" || this.v.is(that.v));
}
if (that instanceof fan.sys.Type)
{
@@ -960,6 +965,10 @@
fan.sys.FuncType.prototype.is = function(that)
{
if (this == that) return true;
+
+ if (that instanceof fan.sys.NullableType)
+ that = that.m_root;
+
if (that instanceof fan.sys.FuncType)
{
// match return type (if void is needed, anything matches)
I agree that Type.fits should probably just ignore nullability. I pushed a fix - its actually much simpler to just handle that in Type.fits before delegating to the internal Type.is implementations.
SlimerDudeTue 29 Sep 2015
Cool, nice fix.
I agree that Type.fits should probably just ignore nullability.
Thanks! But alas, I can't take credit for the idea, see:
SlimerDude Thu 24 Sep 2015
Here's a patch that fixes the quirks around
Type.fits()
when nullable generic types are involved. (I do a lot of reflection and this keeps biting me in the proverbial.)This fixes the bugs in Java and JS as mentioned in:
and brings the two runtimes into alignment.
Some unit tests that exercise all bases:
Note the unit tests unearthed an error in
TypeParser.js
that gave aconsume not defined
error. Here's a fix for that also:brian Mon 28 Sep 2015
I agree that Type.fits should probably just ignore nullability. I pushed a fix - its actually much simpler to just handle that in Type.fits before delegating to the internal Type.is implementations.
SlimerDude Tue 29 Sep 2015
Cool, nice fix.
Thanks! But alas, I can't take credit for the idea, see:
Andy, the following code still fails in the Javascript runtime:
Which can be fixed with the following updated patch:
brian Tue 29 Sep 2015
Thanks, I pushed a fix those JS problems