"stereotyping" (was: Re: Strong Typing (Re: Managing metadata about attribute types)

matz:

But still, there’s no known effective way to define and check
“interface” in a language so dynamic as Ruby. Long way to go.

i disagree. you just require the proper reflection.

I didn’t really get you. Could you explain what “the proper
reflection” is?

yes. there are variouz levels of reflection (or introspection). ruby has very good reflection for OO metaphor. you can examine inside objects, etc. but it has poor method reflection. you can’t easily look at what a method is made of. if you recall from my RCR, as imperfect as it is, it did suggest having methods indexed as arrays of statements (atomic lambdas?) these would of course be nested by scope. anyway, that is another form of reflection.

the type system that you are seeking, suitable to the dynanicism of ruby, can only really come from the proper type of reflection. for ruby that is duck type reflection. – being able of “ask” a method what it would do with the objects passed to it, the result is a method’s duck type signature.

def whatiwould(x, y=0)
x.to_i
y.succ
y + 1
end

method(:whatiwould).duck_signature # → [ [ ‘to_i’ ], [ ‘succ’, ‘+’ ] ]

this tells you what methods the parameters must respond_to.

def shouldi(couldi)
if method(:whatiwould).duck_signature[0].all? {|x| couldi.respond_to?(x.intern)}
whatiwould(couldi)
else
raise “i cannot”
end
end

that’s the general idea anyway. the difficulty lies in accounting for type conversions in the execution stream. but it is not insurmountable. and of course making some sugar so that it is nice.

there would be other method instrospection methods too. like array of default values, etc.

-t0

···

In message “Re: “stereotyping” (was: Re: Strong Typing (Re: Managing metadata about attribute types)” > on 03/11/20, “T. Onoma” transami@runbox.com writes:

Hi,

the type system that you are seeking, suitable to the dynanicism of ruby, can only really come from the proper type of reflection. for ruby that is duck type reflection. – being able of “ask” a method what it would do with the objects passed to it, the result is a method’s duck type signature.

def whatiwould(x, y=0)
x.to_i
y.succ
y + 1
end

method(:whatiwould).duck_signature # → [ [ ‘to_i’ ], [ ‘succ’, ‘+’ ] ]

this tells you what methods the parameters must respond_to.

Interesting. To tell the truth, I thought it as an typing in Ruby
before. But I’m afraid this feature requires definite flow analysis,
which is nearly impossible in the language like Ruby.

def whatiwould(x, flags)
if flags
x = Regex.new(x.join(“|”))
end
x.match(@str)
end
method(:whatiwould).duck_signature # → [[‘join’,‘match’], ] # no!

That’s one of the reason there is no non-functional type inference
language.

						matz.
···

In message “Re: “stereotyping” (was: Re: Strong Typing (Re: Managing metadata about attribute types)” on 03/11/20, “T. Onoma” transami@runbox.com writes: