What do <T> and <S> mean as distinguished from each other.

I noticed that when there is a generic class, there is a , which I decided must use T for "Type". What does the S mean in . I see it in the function definition for the instance function initialize(from: S) -> (S.Iterator, UnsafeMutableBufferPointer.Index) of UnsafeMutableBufferPointer.

Answered by DTS Engineer in 707266022

ScottMichaud_LT wrote:

it sounds like S is what the person who wrote the code chose to mean "some other type that doesn't have to be T".

Right.

Claude31 wrote:

In doc, I find only:

Agreed.

In this declaration:

  • <S> introduces a type S

  • S : Sequence indicates that S must conform to Sequence

  • Element == S.Element indicates that the element type of the sequence, S.Element, must match Element, that is, the element type of the UnsafeMutableBufferPointer

In short, the method in question can populate the buffer from any sequence whose elements match the buffer.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hard to tell from the description, but it sounds like S is what the person who wrote the code chose to mean "some other type that doesn't have to be T".

See:

func test<T, S>(one : T, two : S) -> String
{
    return "\(one) \(two)"
}

print(test(one: Double(1), two: true))

There are a few places where this can be useful, such as Maps or Dictionaries, where the key type can be anything, and the value type can also be some other anything. (Note: T and S can also be the same thing, like with a Dictionary<String,String>.)

Which declaration exactly ?

In doc, I find only:

func initialize<S>(from source: S) -> (S.Iterator, UnsafeMutableBufferPointer<Element>.Index) where Element == S.Element, S : Sequence
Accepted Answer

ScottMichaud_LT wrote:

it sounds like S is what the person who wrote the code chose to mean "some other type that doesn't have to be T".

Right.

Claude31 wrote:

In doc, I find only:

Agreed.

In this declaration:

  • <S> introduces a type S

  • S : Sequence indicates that S must conform to Sequence

  • Element == S.Element indicates that the element type of the sequence, S.Element, must match Element, that is, the element type of the UnsafeMutableBufferPointer

In short, the method in question can populate the buffer from any sequence whose elements match the buffer.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

What do &lt;T&gt; and &lt;S&gt; mean as distinguished from each other.
 
 
Q