Git Product home page Git Product logo

eo's Introduction

Eo

Eo provides syntactic sugars for C++ developers to write Go-like code.

Eo is not recommended for peformance-critical use case. This aims at migrating existing codebase from Go to C++ with minimum changes.

Eo is under active development and its APIs are not stable.

Eo means "I go" in Latin and its pronunciation is "Ay-Oh" like what Freddie Mercury shouted at Live Aid.

License: MIT

Requirements

Build dependencies will be installed by Conan package manager.

  • C++20 (Clang 10+) (GCC support is temporarily disabled)
  • Boost 1.78 (On MacOS, use brew to install boost)
  • fmt
  • scope-lite

Usage

For more examples, refer here.

Goroutine

Goroutine is emulated by C++20 stackless coroutine and awaitable of Boost.Asio.

func<R = void> is an alias type of boost::asio::awaitable<R>.

To generate awaitable function, co_await or co_return keyword should be in function body.

// Go
func f(s string) {
  fmt.Println(s)
}

func main() {
  go f("hello")
  go func() {
    fmt.Println("world")
  }()
}
// C++
func<> f(std::string s) {
  fmt::println(s);
  co_return; // if co_await keyword is used in function body, this can be omitted
}

func<> eo_main() {
  go(f("hello"));
  go([]() -> func<> {
    fmt::println("world");
    co_return;
  });
  co_return;
}

Channel

Channel is emulated by concurrent_channel of Boost.Asio.

Receive operator <-ch and send operator ch <- are replaced by * and << operators.

// Go
func main() {
  ch := make(chan string)
  go func() { ch <- "ping" }()
  msg := <-ch
  fmt.Println(msg);
}
// C++
func<> eo_main() {
  auto ch = make_chan<std::string>();
  go([&]() -> func<> { co_await (ch << "ping"); });
  auto msg = co_await *ch;
  fmt::println(msg);
}

Select statement

Select statement is emulated by awaitable_operators of Boost.Asio.

This has most different syntax from that of Go.

// Go
func f() {
  for {
    select {
    case msg := <-ch:
      fmt.Println(msg)
    default:
      return
    }
  }
}
// C++
func<> f() {
  auto select = Select{*ch, CaseDefault()};
  for (;;) {
    switch (co_await select.index()) {
    case 0:
      auto msg = co_await select.process<0>();
      fmt::println(msg);
      break;
    default:
      co_return;
    }
  }
}

Defer

Defer is emulated by scope-lite that implements experimental std::scope_exit.

For convenience, temporary variable for assigning scope_exit instance is generated by eo_defer macro.

// Go
func f() {
  defer fmt.Println("world")
  fmt.Println("hello")
}
// C++
func<> f() {
  eo_defer([]() { fmt::println("world"); });
  fmt::println("hello");
}

Libraries

Frequently used Go APIs are emulated, but their behaviors are not completely same.

For example, fmt of Go is emulated by fmt of C++, but its behavior follows original C++ API, not that of Go.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.