For example, the following seems to be impossible:
struct Foo<T where T != Int>
{
}
Why does Swift lack this feature?
For example, the following seems to be impossible:
struct Foo<T where T != Int>
{
}
Why does Swift lack this feature?
Explain what the system is supposed to do when you have two of those
struct Foo <T where T != Int> {}
struct Foo <T where T != String> {}
and you say Foo<Something>;
Edit: The bonus questions are "So how do you instantiate a template with no type information (since T != Int doesn't actually tell you what type T is)?"
Perhaps you're being a little unfair. What the system can do is complain of the ambiguity, the same way it currently does when it can't decide which speciailization to use.
Bonus answer: In something simpler like:
struct Foo T> { … }
you also have "no type information". You instantiate it using the type that it matches when it matches.
What prize do I get for answering the bonus question?
Suppose we have a type which takes two generic types A, B and uses them for subscripts. This code would be confusing if A and B were the same type so specifying that A != B would be good.
struct Foo <A, B>
{
subscript (index : A) -> C { }
subscript (index : B) -> D { }
}
Also, this could allow for value-type-only specialization. For example:
protocol Foo
{
}
extension Foo where Self != AnyObject
{
}
The following should answer your question:
protocol X
{
}
extension X
{
func foo()
{
print("I'm an X foo")
}
}
protocol Y
{
}
extension Y
{
func foo()
{
print("I'm a Y foo")
}
}
protocol Z
{
func foo()
}
struct Foo: Z, X, Y
{
}
For your conveniece, is the playground output:
Playground execution failed: /var/folders/rv/mwcdgk492x7g23_0byfwyjtw0000gn/T/./lldb/1444/playground261.swift:44:8: error: type 'Foo' does not conform to protocol 'Z'
struct Foo: Z, X, Y
^
/var/folders/rv/mwcdgk492x7g23_0byfwyjtw0000gn/T/./lldb/1444/playground261.swift:41:10: note: multiple matching functions named 'foo()' with type '() -> ()'
func foo()
^
/var/folders/rv/mwcdgk492x7g23_0byfwyjtw0000gn/T/./lldb/1444/playground261.swift:33:10: note: candidate exactly matches
func foo()
^
/var/folders/rv/mwcdgk492x7g23_0byfwyjtw0000gn/T/./lldb/1444/playground261.swift:20:10: note: candidate exactly matches
func foo() ^