# How does \`associatedtype\` inference work

**URL:** <https://forums.swift.org/t/how-does-associatedtype-inference-work/37017>\
**Category:** Using Swift\
**Created:** [May 29, 2020, 7:57pm UTC](https://forums.swift.org/t/how-does-associatedtype-inference-work/37017 "2020-05-29T19:57:44Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [June 1, 2020, 8:00am UTC](https://forums.swift.org/t/how-does-associatedtype-inference-work/37017/11 "2020-06-01T08:00:56Z")

</div>

> [@jrose](#):
>
> `associatedtype A = Bool` declares that the protocol has an associated type and that it defaults to `Bool` in the absence of any other information.

Thanks, what I still don't understand though, is the following behavior, ie how to answer questions [1] and [2] in this program:

```swift
protocol P {
  associatedtype A = Bool
}
extension P where Self: BinaryFloatingPoint {
  typealias A = Float
}

func foo<T: P>(_: T.Type) {
  print(T.A.self)
}

extension Double: P {}
extension String: P {}

foo(Double.self) // Prints Float
foo(String.self) // Prints Float … [1]: Why/how?

print(Double.A.self) // Prints Float
//print(String.A.self) // Doesn't compile … [2]: Why not? (Error msg makes no sense to me.)

```

I just can't understand the current behavior, I would expect this program to compile and print:

```swift
Float
Bool
Float
Bool

```

You've already addressed this upthread and said the current behavior is wrong:

> [@jrose](#):
>
> (I agree that the behavior is wrong!), but if I recall correctly it's been a longstanding weirdness that associated type checking considers typealiases in protocol extensions _without looking at constraints,_ which is…bizarre, but related to type lookup not doing overload resolution:  
> /.../  
> But I haven't worked on this part of the compiler, so someone else should really answer.  
> /.../  
> EDIT: Oh, and my conclusion is that no one should use typealiases in constrained protocol extensions until this gets worked out, i.e. until there's an actual design. But that's just my opinion.

But I think this program exemplifies what most would perceive as basic/normal usage of Swift generics, ie this does not look very complicated:

```swift
protocol P {
  associatedtype A = Bool
}
extension P where Self: BinaryFloatingPoint {
  typealias A = Float
}

```

and I think most people would agree on what they think it means. So the current behavior / bug(s) seem kind of serious IMHO.

Does your "until there's an actual design" mean that there is no intended behavior for this example program?

I created [this thread](https://forums.swift.org/t/matching-behavior-with-expectations-for-associated-types-and-typealiases-in-constrained-protocol-extensions/37099) in case there isn't, to see if people have the same expectations or not.

---

_[View the full topic](https://forums.swift.org/t/how-does-associatedtype-inference-work/37017)._
