• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Generics Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generics Question


  • Subject: Re: Generics Question
  • From: Michael de Haan  <email@hidden>
  • Date: Sun, 26 Jul 2015 16:30:20 -0700

>
>
> What you really need to do depends on what you’re really trying to achieve.

Sorry, it’s been a long day :-)


I am really trying to understand this.  So….. given this… and I think this will make sense now,


*********
func intFor(s:String) -> Int {

    return Int(s)!

}

func genericFor<T>(s:String) -> T {

    return T(s)!  // error. ’T’ cannot be constructed because it has no accessible initializers
}

let intFromString = intFor("8") //  —>>>  1
let doubleFromStr:Double = genericFor("9") // 2
let anotherIntFromStr:Int = genericFor("7")
***********

(1)   intFromString is straight forward. it returns an integer, 8


(2)  I create a constant, “doubleFromStr” and type it as a double, then call the “generic” "genericFor<T>(s:String) -> T”

thinking that the return

 "T(s)!”

 could do what was done similarly in the call to “intFromString”.

But, as you can see, I get the error shown. So, wondering if what I am trying to do is possible, or just a misreading of “Generics"

Sorry for all the confusion.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Generics Question
      • From: Quincey Morris <email@hidden>
    • Re: Generics Question
      • From: Marco S Hyman <email@hidden>
References: 
 >Generics Question (From: Michael de Haan  <email@hidden>)
 >Re: Generics Question (From: Michael de Haan  <email@hidden>)
 >Re: Generics Question (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: CGFloat and literal floats in Swift
  • Next by Date: Re: CGFloat and literal floats in Swift
  • Previous by thread: Re: Generics Question
  • Next by thread: Re: Generics Question
  • Index(es):
    • Date
    • Thread