ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:66084] [CommonRuby - Feature #10477] [Open] Implicit interfaces
       [not found] <redmine.issue-10477.20141104231652@ruby-lang.org>
@ 2014-11-04 23:16 ` gabriel.sobrinho
  2015-07-09  5:12 ` [ruby-core:69911] [CommonRuby - Feature #10477] " 2851820660
  1 sibling, 0 replies; 2+ messages in thread
From: gabriel.sobrinho @ 2014-11-04 23:16 UTC (permalink / raw)
  To: ruby-core

Issue #10477 has been reported by Gabriel Sobrinho.

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

* Author: Gabriel Sobrinho
* Status: Open
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:69911] [CommonRuby - Feature #10477] Implicit interfaces
       [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
  1 sibling, 0 replies; 2+ messages in thread
From: 2851820660 @ 2015-07-09  5:12 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-07-09  4:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [ruby-core:69911] [CommonRuby - Feature #10477] " 2851820660

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).