# Inconsistency in Type Inference?

**URL:** <https://forums.swift.org/t/inconsistency-in-type-inference/14867>\
**Category:** Using Swift\
**Created:** [July 31, 2018, 7:43am UTC](https://forums.swift.org/t/inconsistency-in-type-inference/14867 "2018-07-31T07:43:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mss](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@mss](https://forums.swift.org/u/mss)\
**Post date:** [July 31, 2018, 7:43am UTC](https://forums.swift.org/t/inconsistency-in-type-inference/14867/1 "2018-07-31T07:43:59Z")

</div>

```
struct SomeStruct: RawRepresentable
{
    typealias RawValue = String
    var rawValue: String
    
    static let aValue = SomeStruct(rawValue: "One")
    static let anotherValue = SomeStruct(rawValue: "Two")
    
    struct SubStruct
    {
        static let yetAnotherValue = SomeStruct(rawValue: "Two")
    }
}

let a: SomeStruct = .aValue
let b: SomeStruct = .SubStruct.yetAnotherValue
let c: SomeStruct = SomeStruct.SubStruct.yetAnotherValue

```

a works but b doesn't. I feel that this is inconsistent. I thought that if the type is inferred, then '.' is the shorthand for 'Type.'. Is it not so?

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 31, 2018, 8:11am UTC](https://forums.swift.org/t/inconsistency-in-type-inference/14867/2 "2018-07-31T08:11:26Z")

</div>

That is currenlty the correct behaviour. How should the compiler know where to start the query?

Consider the a similar example of color types:

```swift
class NSColor {
  static let clear: NSColor { ... }
  var cgColor: CGColor { ... }
}
class UIColor {
  static let clear: UIColor { ... }
  var cgColor: CGColor { ... }
}

let color: CGColor = .clear.cgColor // This is ambiguous

```

In your example even if there is no second `SubStruct` nested anywhere the compiler does not know where to start querying to get the correct and expected result.

In case that I said something wrong, please feel free to correct me.

---

<div class="post-metadata">

**Author:** ![mss](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@mss](https://forums.swift.org/u/mss)\
**Post date:** [July 31, 2018, 8:17am UTC](https://forums.swift.org/t/inconsistency-in-type-inference/14867/3 "2018-07-31T08:17:39Z")

</div>

> [@DevAndArtist](#):
>
> ```swift
> let color: CGColor = .clear.cgColor // This is ambiguous
> 
> ```

In this case colour's type is CGColor and .clear should be inferred as CGColor.clear which is ambiguous.

> [@mss](#):
>
> let b: SomeStruct = .SubStruct.yetAnotherValue

I this case b's type is SomeStruct and so .SubStruct.yetAnotherValue should be inferred as SomeStruct.SubStruct.yetAnotherValue which is what I want.

> [@DevAndArtist](#):
>
> How should the compiler know where to start the query?

The complier just assumes '.' to mean 'TypeOfThisVariable.'

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 31, 2018, 8:24am UTC](https://forums.swift.org/t/inconsistency-in-type-inference/14867/4 "2018-07-31T08:24:50Z")

</div>

> [@mss](#):
>
> The complier just assumes '.' to mean 'TypeOfThisVariable.'

IIRC this topic was discussed quite a few times before. You may want to search the forums for it and open a new discussion thread on the evolution if you want to push it further. If you're going to tackle it please provide new information and summarize everything that has already been said and considered. Please also note that simply bumping old threads is discouraged. Site admins tend to close threads that are bumped.

---

<div class="post-metadata">

**Author:** ![mss](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@mss](https://forums.swift.org/u/mss)\
**Post date:** [July 31, 2018, 9:40am UTC](https://forums.swift.org/t/inconsistency-in-type-inference/14867/5 "2018-07-31T09:40:06Z")

</div>

Oh. I didn't know that. I tried looking for a discussion about before starting this thread and again now, but I wasn't able to find any. Can you point me towards a thread or search term which will help me find the thread?

Edit: Just found [this](https://forums.swift.org/t/my-personal-beef-with-leading-dot-syntax/2055/5)

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [July 31, 2018, 6:48pm UTC](https://forums.swift.org/t/inconsistency-in-type-inference/14867/6 "2018-07-31T18:48:56Z")

</div>

related: [Referring to failable initializer with leading dot syntax](https://forums.swift.org/t/referring-to-failable-initializer-with-leading-dot-syntax/13570)
