swift


Generic function query


After reading this article by Tim Ekl - "Swift Tricks: Searching for Objects by Type", I thought "surely there's a generic function there..." so I coded this:
class One {}
let mixedArray:[Any] = ["One", 1, 1.0, One()]
func filterType1<T>(array: [Any]) -> [T] { // Compiles fine, but cannot be called
return array.flatMap( { $0 as? T })
}
//let f1 = filterType1<Int>(array: mixedArray) // syntax error
//let f1 = filterType1(array: mixedArray) // generic parameter 'T' could not be inferred
On reflection, it's obvious that the compiler can't infer the type of T at compile time if I cannot specify the type in the function name, and so to accomplish this, I need to make the seemingly spurious change:
func filterType2<T>(sampleType: T, array: [Any]) -> [T] {
return array.flatMap( { $0 as? T })
}
let f2 = filterType2(sampleType: 2, array: mixedArray)// [1] as required
However, my question is "why does the definition of the function compile, when it is impossible to call?"
Try this:
let f1: [Int] = filterType1(array: mixedArray) // [1]
Another approach:
let f1 = filterType1(array: mixedArray) as [Int]

Related Links

How to add a Show More/Show Less UIButton to control UITextView
changing background color for the headers in NSTableView
Implementing LazyCollectionProtocol
Add Toolbar JSQMessagesViewController
SQLite.swift: Error during INSERT
How can I get my current date ios Swift?
UTC to local time - wrong result swift
swift - how to call uiviewcontroller method in appdelegate
POST Request Accepting Self Signed Certificate with Swift 3
Sinch Sms Verification 2.0.3 SDK Swift 3
Update annotations loaded with http
How can I create animations as the animations of iMessage in IOS 10?
Using NSFetchedResultsController with a to-many relationship
Updating to CocoaPods 1.1.1 from 0.39 creates linker error
Swift Localization issue
Structure instance property value [duplicate]

Categories

HOME
crystal-reports
ember.js
numpy
pug
erlang
rotation
system-verilog
translation
couchdb-2.0
user
sequelize.js
metronic
pjsip
jboss7.x
game-maker-studio-1.4
cosmicmind
session-timeout
internet-explorer-8
flexlm
backpropagation
marathon
embedly
tarantool
logarithm
compare-and-swap
emv
cgal
thinking-sphinx
aurelia-binding
bxslider
jboss5.x
helix-3d-toolkit
exponential
construct-2
canvasjs
polymorphism
npm-shrinkwrap
pox
strstr
ntfs-mft
parent
intellij-idea-2016
jrules
median
stress-testing
cppunit
rich-text-editor
bnd
spring-data-hadoop
ogre3d
rpostgresql
etherpad
opencpu
rxvt
packagemaker
aescryptoserviceprovider
webkit2
dojox.charting
gherkin
string-parsing
aerogear
group-concat
acm
persistence.xml
matcaffe
android-snackbar
pyopengl
ubercart
sparse-file
debian-based
mdm-zinc
searchkick
magento-1.12
lexicographic
belongs-to
activity-streams
libsndfile
x-ua-compatible
node-imagemagick
java-collections-api
sid
power-law
aquafold
opensocial
ksoap2
json-patch
winrt-httpclient
justgage
mstsc
sunspot-rails
continuous-testing
macruby
concurrent-programming
wchar
throttling
serp
easygui
qtembedded
windows-live-id
sendfile
yajl
internals
evb
standardized

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App