[#46105] [ruby-trunk - Feature #6687][Open] Enumerable#with — "merborne (kyo endo)" <redmine@...>

14 messages 2012/07/02

[#46133] [ruby-trunk - Feature #6688][Open] Object#replace — "prijutme4ty (Ilya Vorontsov)" <prijutme4ty@...>

24 messages 2012/07/03

[#46160] [ruby-trunk - Feature #6693][Open] Don't warn for unused variables starting with _ — "marcandre (Marc-Andre Lafortune)" <ruby-core@...>

15 messages 2012/07/04

[#46200] [ruby-trunk - Bug #6702][Open] Date should be either required or not — "rosenfeld (Rodrigo Rosenfeld Rosas)" <rr.rosas@...>

14 messages 2012/07/05

[#46296] [ruby-trunk - Feature #6717][Open] Method like #instance_eval that returns self (like #tap) — "alexeymuranov (Alexey Muranov)" <redmine@...>

10 messages 2012/07/10

[#46320] [ruby-trunk - Feature #6721][Open] Object#yield_self — "alexeymuranov (Alexey Muranov)" <redmine@...>

25 messages 2012/07/11

[#46339] [ruby-trunk - Bug #6724][Open] waaaaaaant! ( — "zenspider (Ryan Davis)" <redmine@...>

11 messages 2012/07/11

[#46377] [ruby-trunk - Feature #6727][Open] Add Array#rest (with implementation) — "duckinator (Nick Markwell)" <nick@...>

25 messages 2012/07/13

[#46492] [ruby-trunk - Feature #6737][Open] Add Hash#read and alias as #[]. — "trans (Thomas Sawyer)" <transfire@...>

12 messages 2012/07/15

[#46500] [ruby-trunk - Feature #6739][Open] One-line rescue statement should support specifying an exception class — Quintus (Marvin Gülker) <sutniuq@...>

22 messages 2012/07/15

[#46562] [ruby-trunk - Feature #6758][Open] Object#sequence — "merborne (kyo endo)" <redmine@...>

19 messages 2012/07/20

[#46574] [ruby-trunk - Feature #6762][Open] Control interrupt timing — "ko1 (Koichi Sasada)" <redmine@...>

39 messages 2012/07/20

[#46641] [ruby-trunk - Bug #6780][Open] cannot compile zlib module, when cross-compiling. — "jinleileiking (lei king)" <jinleileiking@...>

14 messages 2012/07/23

[#46659] [ruby-trunk - Bug #6783][Open] Infinite loop in inspect, not overriding inspect, to_s, and no known circular references. Stepping into inspect in debugger locks it up with 100% CPU. — "garysweaver (Gary Weaver)" <garysweaver@...>

8 messages 2012/07/23

[#46792] [ruby-trunk - Bug #6799][Open] Digest::*.hexdigest returns an ASCII-8BIT String — "Eregon (Benoit Daloze)" <redmine@...>

11 messages 2012/07/26

[#46799] [ruby-trunk - Feature #6801][Open] String#~ for a here document — "merborne (kyo endo)" <redmine@...>

12 messages 2012/07/27

[#46829] [ruby-trunk - Feature #6806][Open] Support functional programming: forbid instance/class variables for ModuleName::method_name, allow for ModuleName.method_name — "alexeymuranov (Alexey Muranov)" <redmine@...>

7 messages 2012/07/28

[#46832] [ruby-trunk - Bug #6807][Open] Can't compile ruby without ruby — "devcurmudgeon (Paul Sherwood)" <storitel@...>

13 messages 2012/07/28

[#46834] [ruby-trunk - Feature #6808][Open] Implicit index for enumerations — "trans (Thomas Sawyer)" <transfire@...>

15 messages 2012/07/28

[#46838] [ruby-trunk - Bug #6810][Open] `module A::B; end` is not equivalent to `module A; module B; end; end` with respect to constant lookup (scope) — "alexeymuranov (Alexey Muranov)" <redmine@...>

17 messages 2012/07/28

[#46896] (Half-baked DRAFT) new `require' framework — SASADA Koichi <ko1@...>

Hi,

22 messages 2012/07/31

[ruby-core:46285] [ruby-trunk - Feature #6711] Optional typing and method overloading

From: "jballanc (Joshua Ballanco)" <jballanc@...>
Date: 2012-07-09 20:26:03 UTC
List: ruby-core #46285
Issue #6711 has been updated by jballanc (Joshua Ballanco).


Just a humble suggestion, but it seems like what you want is not so much method overloading as it is multidispatch. Clojure has a feature like this, and there is a gem that simulates the same: https://github.com/psantacl/ruby-multimethods . If you'd like to re-work your proposal, you might want to think about how multidispatch could be better supported by the Ruby language.
----------------------------------------
Feature #6711: Optional typing and method overloading
https://bugs.ruby-lang.org/issues/6711#change-27904

Author: rosenfeld (Rodrigo Rosenfeld Rosas)
Status: Rejected
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 3.0


Today I woke up with some ideas to overcome some of the features I miss in Ruby. I've searched for similar issues and I found this #5583, which was rejected.

But my idea is a bit different and also concerns about method overloading. So I decided to open a new issue. I'll be also creating another feature request soon for introducing super! keyword for overrides of methods by reopening classes.

#5583 wants the checks to be made at compile time, while I think it should be a dynamic check instead.

For example:

class A
  def some_method(a)
    "a"
  end

  def some_method(a # Numeric)
    "a is a number"
  end

  def some_method(a, b)
    "untyped a and b"
  end

  def some_method(a, b # String)
    "typed b"
  end

  def some_method(a # String, b = 2)
    "typed a"
  end
end

class B < A
  def! some_method(*args) # overrides all overloaded methods - *args is not required for this
    if args[0].is_a? Number
      super(args[0])
    else
      'do something else'
    end
  end
end

The rule to decide over multiple alternatives should be by order:

1 - max number of better matched argument types # Float is a better match for 1.2 than Numeric
2 - max number of matched argument types
3 - max number of consecutive matched argument types

Example:

a = A.new
b = B.new

b.some_method == 'do something else'
b.some_method(1.3) == 'a is a number'
b.some_method('s') == 'do something else'
a.some_method('s') == 'typed a'
a.some_method('s', 's') == 'typed a'
a.some_method(:s, 's') == 'typed b'
a.some_method(:s) == 'a'
a.some_method(1, :s) == 'untyped a and b'

Current method and send are not affected. Argument-matching checks would happen at runtime

The goal is to make programmers happier, not be perform better.

m = a.method :some_method
m.call # raise arguments mismatch exception
m.call(:a) == 'a'

To get a specific method one could ask for it like:

m = a.method :some_method, nil, String # some_method(a, b # String)

We should also introduce "methods" for getting a list of all named methods defined:

# HTTP server dispatcher class example:

def handle_http_request(params)
  methods = controller.methods :some_method
  raise "Overloaded actions are not supported" unless methods.size == 1
  action = methods.first
  method_arguments = []
  action.arguments.each do |name, type, default_value = nil|
    hk = params.has_key? name
    (method_arguments << hk ? params[name] : default_value; next) unless hk && !type.nil?
    method_arguments << method(:bind_value, type).call params[name], type
  end
  action.call *method_arguments
end

def bind_value(value, type)
  value
end

def bind_value(value, type # Integer)
  value.to_i
end

def bind_value(value, type # Float)
  value.to_f
end

def bind_value(value, type # Numeric) # BigDecimal and Numeric would match for instance
  BigDecimal.new value
end

def bind_value(value, type # Date)
  Date.parse value
end

#...



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

In This Thread