ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: 2851820660@qq.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:69911] [CommonRuby - Feature #10477] Implicit interfaces
Date: Thu, 09 Jul 2015 05:12:00 +0000	[thread overview]
Message-ID: <redmine.journal-53330.20150709051200.444bcbabc9523383@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-10477.20141104231652@ruby-lang.org

Issue #10477 has been updated by 11 22.


http://www.software-rating.com/
http://www.smartlogi.com/  
http://www.shareorder.com/  
http://www.gzs168.com/  
http://www.aimooimage.com/    
http://www.chinatowngate.net/

http://www.inspiredhypnosis.co.uk/daocplat.html
http://the303plan.com/tibiagoldforsale.html

----------------------------------------
Feature #10477: Implicit interfaces
https://bugs.ruby-lang.org/issues/10477#change-53330

* Author: Gabriel Sobrinho
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Hello guys,

I would to suggest us to discuss about implementing implicit interfaces on Ruby like Go.

> Go does not have classes. However, you can define methods on struct types. The method receiver appears in its own argument list between the func keyword and the method name.

This means you can specify a implicit interface where the implementation packages and packages that define the interfaces neither depends on the other.

That keeps the concept of duck typing but adds a extra layer of interface security to the language instead of relying on `NoMethodError` exceptions.

Go usage example:

``` go
type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
```

In Ruby it could something like that:

``` ruby
interface Cache
  def get(key, default = nil)
  def set(key, value, ttl = nil)
  def delete(key)
end

class App
  def initialize(cache_store Cache)
    @cache_store = cache_store
  end

  delegate :get, :set, :delete, :to => :@cache_store
end
```

In this case a `NoMethodError` would never occur on `App#get`, `App#set` and `App#delete`.

If you think about service objects, you may have things like that:

``` ruby
class Buy
  def self.finish(object)
    object.store(
      fine: FineCalculator.calculate(object.value, Date.current)
      interest: InterestCalculator.calculate(object.value, Date.current)
      expedient: ExpedientCalculator.calculate(object.value, Date.current)
    )

    BuyMailer.deliver(to: object.buyer, object: object)
  end
end
```

In a case of a failure on calling `object.buyer`, the `object.store` has already happened and may affect the system in a bad way, which may not be acceptable.

Using a implicit interface it would never happen:

``` ruby
interface Purchasable
  def store(attrs)
  def value
  def buyer
end

class Buy
  def self.finish(object Purchasable)
    object.store(
      fine: FineCalculator.calculate(object.value, Date.current)
      interest: InterestCalculator.calculate(object.value, Date.current)
      expedient: ExpedientCalculator.calculate(object.value, Date.current)
    )

    BuyMailer.deliver(to: object.buyer, object: object)
  end
end
```

I think it's a great idea of Go that would be of benefit in Ruby.

Probably there is better usage cases, sorry about that, but the concept is to have implicit interfaces on libraries that we publish for everyone (gems).

Think about complex interfaces like [capybara drivers](https://github.com/jnicklas/capybara/blob/master/lib/capybara/driver/base.rb), [active support cache drivers](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/cache.rb#L484-L500) and etc.

How it sounds?

_Reference: http://programmers.stackexchange.com/questions/197356/how-does-go-improve-productivity-with-implicit-interfaces-and-how-does-that-c_



-- 
https://bugs.ruby-lang.org/

      parent reply	other threads:[~2015-07-09  4:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-10477.20141104231652@ruby-lang.org>
2014-11-04 23:16 ` [ruby-core:66084] [CommonRuby - Feature #10477] [Open] Implicit interfaces gabriel.sobrinho
2015-07-09  5:12 ` 2851820660 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-53330.20150709051200.444bcbabc9523383@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).