I don't really get it why the compiler is to complain about the if block not 
having an else in this case. That seems like a bug that will push us to write 
unnecessary code. The compiler is not capable of inferring there are more 
conditions to evaluate in an if statement opposed to a switch statement. Even 
with the proposed union the compiler cannot infer the need for an else block 
here because the programmer might not want to do anything else with whatever 
doesn't match the first condition.

On the other hand, if you intend to add to this proposal that a switch 
statement could evaluate the type of the value like the following code, I'd 
agree with you. I'm just not sure one could do this as of today.

func input(value: (A | B | C)) {
   switch value.type {
      case A:
         ...
      case B:
         ...
      default:
         // here the compiler knows I didn't cover all possible types
         // adding 'case C' here makes 'default' unnecessary
   }
}

I would add to this discussion if this would be the best syntax for the 
proposed type. I'd think about 'union<A, B, C>' as it matched other existing 
syntax and is a bit more explicit about what's going on the code.

> On 16 May 2016, at 7:55 am, Cao Jiannan via swift-evolution 
> <[email protected]> wrote:
> 
> Hi Austin,
> 
> let me repeat the example so that clarify my point from this example.
> 
> protocol cannot do this:
> 
> func input(value: ProtocolForABC) {
>     print(value.someCommonProperty)
> 
>     if value is A {
>         
>     } else if value is B {
>         
>     } else if value is C {
>         
>     } else {
>         // There no other cases, but compiler will not trigger a warning.
>     }
> }
> 
> The compiler will not know your protocol is only conformed to these three 
> classes.
> So the else block will not trigger a warning.
> 
> - Jiannan
> 
> 
>> 在 2016年5月16日,18:37,Austin Zheng <[email protected]> 写道:
>> 
>> I'm sorry, but I don't understand the point you are trying to make.
>> 
>> If you pass in a value of type (A | B | C) to a function, what might you 
>> want to do with that value?
>> 
>> If you want to do one thing if the value is type A, something else if the 
>> value is type B, and something else if the value is type C, then you need to 
>> switch or otherwise type check the value at runtime. You can't get around 
>> this, no matter whether you use enums, protocols, generics, or union type.
>> 
>> If you want it to do something that A, B, and C all support, use a generic 
>> and/or a protocol. In this case limiting the inputs to only those three 
>> types is probably a design smell. The whole point of a shared interface is 
>> that it only matters that the interface is properly implemented by a type, 
>> not what that type is.
>> 
>> If you don't care about doing anything with the value, just make your 
>> function generic: func<T>(input: T).
>> 
>> Austin
> 
> _______________________________________________
> swift-evolution mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to