Project

General

Profile

Actions

Feature #6711

closed

Optional typing and method overloading

Added by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago. Updated over 12 years ago.

Status:
Rejected
Target version:
[ruby-core:<unknown>]

Description

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

#...


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #5583: Optionally typingRejectedmatz (Yukihiro Matsumoto)11/07/2011Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0