summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2017-04-28 18:26:33 +0300
committerZahary Karadjov <zahary@gmail.com>2017-04-28 18:26:41 +0300
commit5c678e284862e6b52b0dd4118ef857d7b6b17ef9 (patch)
treee1b70b74aab6e324d67660fc6b3feae4ef069467
parent72f42ff95f08e256344913f1577a17ba46a374ce (diff)
downloadNim-5c678e284862e6b52b0dd4118ef857d7b6b17ef9.tar.gz
alternative fake covariance based on converters
-rw-r--r--tests/generics/tfakecovariance.nim26
-rw-r--r--tests/generics/tptrinheritance.nim2
2 files changed, 25 insertions, 3 deletions
diff --git a/tests/generics/tfakecovariance.nim b/tests/generics/tfakecovariance.nim
index bca285283..0920cb504 100644
--- a/tests/generics/tfakecovariance.nim
+++ b/tests/generics/tfakecovariance.nim
@@ -35,8 +35,8 @@ reject wantsSpecificContainers(derived)
 # Now, let's make a more general solution able to catch all derived types:
 
 type
-  DerivedFrom[T] = concept type A
-    var derived: ref A
+  DerivedFrom[T] = concept type D
+    var derived: ref D
     var base: ref T = derived
 
 proc wantsDerived(x: DerivedFrom[BaseObj]) = discard
@@ -54,3 +54,25 @@ accept wantsDerivedContainer(derivedContainer)
 
 reject wantsDerivedContainer(nonDerivedContainer)
 
+# The previous solutions were solving the problem for a single overload.
+# Let's solve it for multiple overloads by introducing a converter:
+
+type
+  OtherContainer[T] = object
+
+proc wantsBaseContainer1(c: OtherContainer[BaseObj]) = discard
+proc wantsBaseContainer2(c: OtherContainer[BaseObj]) = discard
+
+converter derivedToBase(c: OtherContainer[DerivedFrom[BaseObj]]): OtherContainer[BaseObj] = discard
+
+block:
+  var baseContainer: OtherContainer[BaseObj]
+  var derivedContainer: OtherContainer[DerivedObj]
+  var nonDerivedContainer: OtherContainer[NonDerivedObj]
+
+  accept wantsBaseContainer1(derivedContainer)
+  reject wantsBaseContainer1(nonDerivedContainer)
+
+  accept wantsBaseContainer2(derivedContainer)
+  reject wantsBaseContainer2(nonDerivedContainer)
+
diff --git a/tests/generics/tptrinheritance.nim b/tests/generics/tptrinheritance.nim
index 1e1115fa5..221b8777b 100644
--- a/tests/generics/tptrinheritance.nim
+++ b/tests/generics/tptrinheritance.nim
@@ -10,7 +10,7 @@ proc newMutableArrayAbstract*(): NSMutableArrayAbstract = discard
 template newMutableArray*(T: typedesc): NSMutableArray[T] =
   cast[NSMutableArray[T]](newMutableArrayAbstract())
 
-proc writeObjects*(p: NSPasteboard, o: ptr NSArray[NSPasteboardItem]) = discard
+proc writeObjects*(p: NSPasteboard, o: NSArray[NSPasteboardItem]) = discard
 
 let a = newMutableArray NSPasteboardItem
 var x: NSMutableArray[NSPasteboardItem]