Skip to content

Comment on Scaling Scala vs Java

Comments

I dislike how the Java Promises and the Scala code unnecessarily hide the reality of the computation from the developer, take a Go version as counterpoint:

  func stockForProdsTO(uid int, timeout time.Duration) (*Stock, error) {
  	stockCh := make(chan *Stock, 1)
  	go func() {
  		orders := ordersForUser(userById(uid).email)
  		stockCh <- stockForProds(prodsForOrders(orders))
  	}()
  	select {
  		case result := <-stockCh:
  			return result, nil
  		case <-time.After(timeout):
  			return nil, errors.New("Could not get stock for product before timeout.")
  	}
  }
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.