Hacker News new | past | comments | ask | show | jobs | submit login

How much different is this from slicing in C++?

IE:

  #include <iostream>
  using namespace std;

  struct Greeter {
    void greet() {
        cout << "Hello, World!" << endl;
    }
  };

  struct BaseGreeter: Greeter {};

  struct LazyGreeter: BaseGreeter {
    void greet() {
        cout << "sup" << endl;
    }
  };

  int main() {
    Greeter greeter = LazyGreeter();
    greeter.greet(); // Prints "Hello, World"

    return 0;
  }



In C++ world this is expected behavior AND it's documented. But in swift, protocols cannot declare default implementations but it's possible to define implementation to protocol using extensions. So in C++, it might look like this

  struct Greeter {
    virtual void greet() {
      cout << "Hello, world!" << endl;
    }
  };
  
  struct BaseGreeter: Greeter {};
  
  struct LazyGreeter: BaseGreeter {
    void greet() override {
      cout << "sup" << endl;
    }
  };
  
  int main() {
    Greeter *greeter = new LazyGreeter();
    greeter->greet(); // In C++ prints "sup", but in Swift "Hello, World!"
    delete greeter;
    return 0;
  }




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: