6 ms·
Wow, Heisen-Swift, where the Heisenbugs are in the language specification! What an object is, which is roughly equivalent to its observed behavior, should neve
by mpweiher 8y ago
Wow, Heisen-Swift, where the Heisenbugs are in the language specification!
What an object is, which is roughly equivalent to its observed behavior, should never depend on how it is declared.
let greeter = LazyGreeter()
let greeter1: Greeter = greeter
print(greeter)
print(greeter1)
greeter.greet()
greeter1.greet()
UPDATE: just in case it's not clear, this prints the following:
greeter.LazyGreeter
greeter.LazyGreeter
sup
Hello, World!
So the same object responds differently to the same message, depending on how it is declared. Yikes!
- wool_gather 8y agoIt seems this is what you get when you decide dynamic dispatch is a demon that must be cast out. :/ It's a good example of Swift's at-times-infuriating insistence on chasing performance at the expense of developer ergonomics. That said, I'm sure it will be mediated eventually (although probably with more compiler annotations).
- mpweiher 8y ago> infuriating insistence on chasing performance at the expense of developer ergonomics It would be slightly less infuriating had that chase actually delivered, but it dramatically has not.
- arcticbull 8y agoThe problem here is that Swift is sort of this hybrid between two disparate worldviews, the old Objective-C world where inheritance is cool and dispatch is dynamic and the new world where inheritance is not even permitted and traits are used to provide dynamic dispatch. It's trying to be everything to everyone. This is one of those crossover points where the seams don't meet. If this was all dynamic, it would be fine because the messages would be dispatched properly. If this was all new world, you wouldn't even be allowed to subclass, would refer to your trait explicitly, and again, everything would work fine. This is a fundamental structural issue with the language, philosophically, and will definitely cost programmer productivity. I'd argue this isn't a problem so much as behaves correctly due to insufficient planning. It's not that they're taking a position on dynamic dispatch, it's that they're trying to take both positions, likely in support of Objective-C compatibility. IMO dynamic dispatch has caused way more programmer productivity issues than anything else - you write code the compiler can't validate because you explicitly chose not to give it the information it needs to do it's job. It can't tell you your code is right. Nothing really can. The new world is a better place, Go and Rust have it right, inheritance is dead.
- anoncareer0212 8y agoThis is far too far, and a lot of it is wrong
- perl4ever 8y agoIf you could elaborate on what is wrong with it, that would be helpful, compared to just dismissing it.
- arcticbull 8y agoYeah I’d love to have a conversation about your thoughts here!
- mpweiher 8y agoWhile inheritance is OK, and useful in programming-by-difference scenarios, Objective-C style polymorphism has always de-emphasized inheritance. Dynamic dispatch is central to both the most productive and largest-scale software systems/environments in the world. If you think the compiler can tell you everything, you're in for a world of hurt, and if you think you can statically type-check the world: good luck. Again, inheritance is useful, though overused in static languages that only allow polymorphism together with inheritance.
- saagarjha 8y agoObjective-C deemphasizes formal inheritance. Class clusters and informal protocols generally substitute for some of the benefits that inheritance can give.
- wool_gather 8y agoI guess fundamentally, this is the question I have about this situation: would you ever, in real code, want dispatch to go straight back to the default implementation when you have in hand a value with a custom implementation? I may be having a failure of imagination; but I certainly haven't ever seen a case where I'd want that.
- mjburgess 8y agoCould you clarify... the "declaration" includes the type right? So why should behavior not depend on the (static) type of the term? That's a very standard use of static typing. It seems like you're coming from a dynamic background. There really isn't any such principle or rule here, only it seems, unfamiliarity with type systems.
- torpidor 8y ago> What an object is, which is roughly equivalent to its observed behavior, should never depend on how it is declared. This is actually a statement that strong typing should not exist. A "declaration" (really a type declaration) indicates the type, and if observed behavior cannot depend on the types there is not much point to types. Or stated positively: in a strongly typed language, the types are part of 'what an object is'. Or, stated by example: let a: Int = 3 let b: Float = 3 print(a/2) //prints 1 print(b/2) //prints 1.5 Both a and b are "the same" value, insofar as they are `==` and they model the same underlying mathematical concept. But behavior varies because they differ in type. OP's snippet actually works quite a lot differently than the way you may expect; LazyGreeter.greet() and Greeter.greet() are two completely independent functions that share the same name. Name resolution is a complex topic in any language, but in Swift if you wanted to override another function you would say `override`, in which case the compiler will complain there is nothing you can override in `BaseGreeter` at which point you will understand the whole mistake. There is actually no way to "pick" LazyGreeter's implementation of the unrelated function (as perhaps you expect). The only function we can call on a Greeter is Greeter.greet (or its overloads, and there are none). So if Greeter.greet did not exist, we would not get LazyGreeter.greet() but rather a type error. I do think a case can be made that we need a similar `implements` keyword like `override` to check that we are implementing a protocol requirement.
- Black-Plaid 8y agoInterestingly, in Swift, you Cannot compare `a == b` as the compiler tells you the two types are incompatible.
- wz1000 8y agoHaskell has no problem with this(though its default Num hierarchy is sorely lacking) Prelude> a = 1 Prelude> a/2 0.5 Prelude> :t a/2 a/2 :: Fractional a => a Prelude> b = 1 :: Int Prelude> b/2 <interactive>:6:1: error: • No instance for (Fractional Int) arising from a use of ‘/’ • In the expression: b / 2 In an equation for ‘it’: it = b / 2 Prelude> b `div` 2 0 Prelude> :t div div :: Integral a => a -> a -> a
- 8y ago
- scarface74 8y agoI’m trying to wrap my C# brain around this... Is this similar to the C# construct.... interface IGreeter { void Greet()} class BaseGreeter :IGreeter { public void Greet {Console.WriteLine(“Hello World”)} public void IGreeter:Greet {Console.Writeline(“sup”)} } } Greeter greet1 = new Greeter(); IGreeter greet2 = greet1(); Console.Writeline(greet1 == greet2) greet1.Greet() greet2.Greet() Would print I believe. true Hello World sup