Type inference of array element type


(Toni Suter) #1

Hi,

If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
the compiler will infer the type Array<Optional<Int>> for that variable:

let arr = [1, nil, 3]
print(type(of: arr)) // Array<Optional<Int>>

However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
I get a compile error:

let arr = [(1, false), nil, (3, true)] // error: type of expression is ambiguous without more context
print(type(of: arr))

Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?

Thanks and best regards,
Toni


(Mark Lacey) #2

Hi,

If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
the compiler will infer the type Array<Optional<Int>> for that variable:

let arr = [1, nil, 3]
print(type(of: arr)) // Array<Optional<Int>>

However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
I get a compile error:

let arr = [(1, false), nil, (3, true)] // error: type of expression is ambiguous without more context
print(type(of: arr))

Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?

Offhand it seems like we should be able to properly handle this. Can you open a bug report at bugs.swift.org <http://bugs.swift.org/>?

Mark

···

On Mar 24, 2017, at 3:08 AM, Toni Suter via swift-users <swift-users@swift.org> wrote:

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #3

IMO this is a boundary problem.
How far do you want to go in letting the compiler deduce the actual type?
It is possible to make very elaborate constructs that would basically default to a complex tuple/array/dictionary construct with only Any?’s in them. (well, the dict would require a Hashable too)

Besides, the recent discussion on compile times illustrates another angle to this problem: if type inference is used extensively, compile times go to infinite…

So while I do not know if this is a bug or not, I would recommend not to use it anyhow.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

···

On 24 Mar 2017, at 11:08, Toni Suter via swift-users <swift-users@swift.org> wrote:

Hi,

If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
the compiler will infer the type Array<Optional<Int>> for that variable:

let arr = [1, nil, 3]
print(type(of: arr)) // Array<Optional<Int>>

However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
I get a compile error:

let arr = [(1, false), nil, (3, true)] // error: type of expression is ambiguous without more context
print(type(of: arr))

Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Toni Suter) #4

Ok, I submitted a bug report (https://bugs.swift.org/browse/SR-4347) and I'll try to fix it in the
next few days.

Thanks and best regards
Toni

···

Am 24.03.2017 um 18:51 schrieb Mark Lacey <mark.lacey@apple.com>:

On Mar 24, 2017, at 3:08 AM, Toni Suter via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hi,

If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
the compiler will infer the type Array<Optional<Int>> for that variable:

let arr = [1, nil, 3]
print(type(of: arr)) // Array<Optional<Int>>

However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
I get a compile error:

let arr = [(1, false), nil, (3, true)] // error: type of expression is ambiguous without more context
print(type(of: arr))

Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?

Offhand it seems like we should be able to properly handle this. Can you open a bug report at bugs.swift.org <http://bugs.swift.org/>?

Mark

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #5

Btw, I just looked it up and it seems to me that inference only works for literals. Which probably means that tuples are out.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

···

On 24 Mar 2017, at 11:22, Rien via swift-users <swift-users@swift.org> wrote:

IMO this is a boundary problem.
How far do you want to go in letting the compiler deduce the actual type?
It is possible to make very elaborate constructs that would basically default to a complex tuple/array/dictionary construct with only Any?’s in them. (well, the dict would require a Hashable too)

Besides, the recent discussion on compile times illustrates another angle to this problem: if type inference is used extensively, compile times go to infinite…

So while I do not know if this is a bug or not, I would recommend not to use it anyhow.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

On 24 Mar 2017, at 11:08, Toni Suter via swift-users <swift-users@swift.org> wrote:

Hi,

If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
the compiler will infer the type Array<Optional<Int>> for that variable:

let arr = [1, nil, 3]
print(type(of: arr)) // Array<Optional<Int>>

However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
I get a compile error:

let arr = [(1, false), nil, (3, true)] // error: type of expression is ambiguous without more context
print(type(of: arr))

Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Toni Suter) #6

Hmm, I don't know. It also works with other nominal types. For example:

struct S {
  var x: Int
  var y: Int
}
let s1 = S(x: 0, y: 1)
let s2 = S(x: 2, y: 3)
let arr = [s1, nil, s2]
print(type(of: arr)) // Array<Optional<S>>

···

Am 24.03.2017 um 11:30 schrieb Rien <Rien@Balancingrock.nl>:

Btw, I just looked it up and it seems to me that inference only works for literals. Which probably means that tuples are out.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

On 24 Mar 2017, at 11:22, Rien via swift-users <swift-users@swift.org> wrote:

IMO this is a boundary problem.
How far do you want to go in letting the compiler deduce the actual type?
It is possible to make very elaborate constructs that would basically default to a complex tuple/array/dictionary construct with only Any?’s in them. (well, the dict would require a Hashable too)

Besides, the recent discussion on compile times illustrates another angle to this problem: if type inference is used extensively, compile times go to infinite…

So while I do not know if this is a bug or not, I would recommend not to use it anyhow.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

On 24 Mar 2017, at 11:08, Toni Suter via swift-users <swift-users@swift.org> wrote:

Hi,

If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
the compiler will infer the type Array<Optional<Int>> for that variable:

let arr = [1, nil, 3]
print(type(of: arr)) // Array<Optional<Int>>

However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
I get a compile error:

let arr = [(1, false), nil, (3, true)] // error: type of expression is ambiguous without more context
print(type(of: arr))

Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #7

I always yield to proof :wink:

hope someone more knowledgable chips in...

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

···

On 24 Mar 2017, at 11:53, Toni Suter <tonisuter@me.com> wrote:

Hmm, I don't know. It also works with other nominal types. For example:

struct S {
  var x: Int
  var y: Int
}
let s1 = S(x: 0, y: 1)
let s2 = S(x: 2, y: 3)
let arr = [s1, nil, s2]
print(type(of: arr)) // Array<Optional<S>>

Am 24.03.2017 um 11:30 schrieb Rien <Rien@Balancingrock.nl>:

Btw, I just looked it up and it seems to me that inference only works for literals. Which probably means that tuples are out.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

On 24 Mar 2017, at 11:22, Rien via swift-users <swift-users@swift.org> wrote:

IMO this is a boundary problem.
How far do you want to go in letting the compiler deduce the actual type?
It is possible to make very elaborate constructs that would basically default to a complex tuple/array/dictionary construct with only Any?’s in them. (well, the dict would require a Hashable too)

Besides, the recent discussion on compile times illustrates another angle to this problem: if type inference is used extensively, compile times go to infinite…

So while I do not know if this is a bug or not, I would recommend not to use it anyhow.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

On 24 Mar 2017, at 11:08, Toni Suter via swift-users <swift-users@swift.org> wrote:

Hi,

If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
the compiler will infer the type Array<Optional<Int>> for that variable:

let arr = [1, nil, 3]
print(type(of: arr)) // Array<Optional<Int>>

However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
I get a compile error:

let arr = [(1, false), nil, (3, true)] // error: type of expression is ambiguous without more context
print(type(of: arr))

Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Toni Suter) #8

Thanks anyway! :slight_smile:

···

Am 24.03.2017 um 12:03 schrieb Rien <Rien@balancingrock.nl>:

I always yield to proof :wink:

hope someone more knowledgable chips in...

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

On 24 Mar 2017, at 11:53, Toni Suter <tonisuter@me.com> wrote:

Hmm, I don't know. It also works with other nominal types. For example:

struct S {
var x: Int
var y: Int
}
let s1 = S(x: 0, y: 1)
let s2 = S(x: 2, y: 3)
let arr = [s1, nil, s2]
print(type(of: arr)) // Array<Optional<S>>

Am 24.03.2017 um 11:30 schrieb Rien <Rien@Balancingrock.nl>:

Btw, I just looked it up and it seems to me that inference only works for literals. Which probably means that tuples are out.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

On 24 Mar 2017, at 11:22, Rien via swift-users <swift-users@swift.org> wrote:

IMO this is a boundary problem.
How far do you want to go in letting the compiler deduce the actual type?
It is possible to make very elaborate constructs that would basically default to a complex tuple/array/dictionary construct with only Any?’s in them. (well, the dict would require a Hashable too)

Besides, the recent discussion on compile times illustrates another angle to this problem: if type inference is used extensively, compile times go to infinite…

So while I do not know if this is a bug or not, I would recommend not to use it anyhow.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

On 24 Mar 2017, at 11:08, Toni Suter via swift-users <swift-users@swift.org> wrote:

Hi,

If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
the compiler will infer the type Array<Optional<Int>> for that variable:

let arr = [1, nil, 3]
print(type(of: arr)) // Array<Optional<Int>>

However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
I get a compile error:

let arr = [(1, false), nil, (3, true)] // error: type of expression is ambiguous without more context
print(type(of: arr))

Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?

Thanks and best regards,
Toni

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users