Again a testproject in Visual Studio with essentially 2 functions:
func testarray(){
let array = [2,2,1,1,3]
var sum = 0
for i in array{
sum += i
}
/* let st:Iterable
st = array*/
let count:Int = countthrough(c: array)//,dummy: Int(0))
assert(sum == 9,"array works")
assert(count == array.count && count == 5)
}
func countthrough<T:Iterable>(c:T)->Int{
var ret = 0
for elt in c{
ret += 1
}
return ret
}
On compile, i get the error
(E210) Generic parameter doesn’t fulfill constraint “Iterable<swift.Array<Integer!>>” for "T"
at the call to countthrough.
So, i thought: ‘Maybe the array doesn’t conform to Iterable’,
so i tried this version of testarray:
func testarray(){
let array = [2,2,1,1,3]
var sum = 0
for i in array{
sum += i
}
let st:Iterable
st = array
let count:Int = countthrough(c: st)//,dummy: Int(0))
//asserts left out
}
Then on compile, I get again the error:
(E210) Generic parameter doesn’t fulfill constraint “Iterable<Iterable>” for “T”.
Then I gave some hints to the compiler:
func testarray(){
let array = [2,2,1,1,3]
var sum = 0
for i in array{
sum += i
}
let st:Iterable<Int>
st = array
let count:Int = countthrough(c: st)//,dummy: Int(0))
assert(sum == 9,"array works")
assert(count == array.count && count == 5)
}
func countthrough<T:Iterable<Int>>(c:T)->Int{
var ret = 0
for elt in c{
ret += 1
}
return ret
}
which worked, but NOT if you do the call to countthrough like this:
let count = countthrough(c:array)
(which results in E210 again)
but kind of defeats the purpose of having generics,so i trial and errored until I had this version:
func testarray(){
let array = [2,2,1,1,3]
var sum = 0
for i in array{
sum += i
}
let st:Iterable<Int>
st = array
let count:Int = countthrough(c: st,dummy: Int(0))
assert(sum == 9,"array works")
assert(count == array.count && count == 5)
}
func countthrough<I,T:Iterable<I>>(c:T,dummy:I)->Int{
var ret = 0
for elt in c{
ret += 1
}
return ret
}
Note, that removing basically any of the redundant type hints,
causes the compiler to emit a E210 or some internal error.
Version Info:
Microsoft Visual Studio 2015 Shell (Integrated)
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.01590
Installed Version: IDE Standard
RemObjects Elements 9.0.97.2071
RemObjects Elements (Oxygene, C# and Silver) for .NET, Cocoa and Java.
Copyright 2003-2016 RemObjects Software, LLC. All rights reserved.
http://www.remobjects.com/elements
RemObjects Elements leverages the LLVM compiler backend:
Copyright © 2003-2016 University of Illinois at Urbana-Champaign. All rights reserved.
http://llvm.org
RemObjects Everwood 4.7.79.695
RemObjects Everwood
Copyright RemObjects Software, LLC 2002-2016. All Rights Reserved.
http://www.remobjects.com/everwood