lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Karel Tuma kirjoitti 23.10.2006 kello 15.54:

hi,

what about multimethod dispatch based on types or even constraints,
lispers are constantly bragging about:

I don't know much about Lisp-etc. - goo there are people, who do. :)

tab.do_something(num foo, num bar)
tab.do_something(string blah, num baz)

could be sugar for

tab.do_something_number_number(foo,bar)
tab.do_something_string_number(blah,baz)

and calling protocol:
tab[do_something.."_"..type(arg1).."_"..type(arg2)](arg1,arg2)

tab.do_something( foo :num_or_string, bar :num )

'num_or_string' is a fancy (probably customized) function living as 'assert.num_or_string'. The constraints mechanism is giving no precedence to built-in types, _any_ constraint names can be used and they cause the corresponding 'assert.*' function to be called (or an error if no such is defined).

An assertion function can also _change_ the type of the value passing through it; this is meaningful for cases s.a. string->num conversion to allow placing "20" in a variable/parameter taking in numeric values (this, of course, can be changed simply by redeclaring 'assert.number' :).

I'm wanting to place all this out for grabs, perhaps by the end of the week I can. I just know too little about inner-inner-level of Lua core, but... learning.


similiarly as for argument type constraints, this is partially implementable using
metatables, but not to the degree i'd call satisfactory.

For arguments are just variables, as are function parameters.


this is not trivial, if not impossible, to implement into current
parser.

<lua_vs_lisp_rants>
but having this along with first-class value macros (could be
possibly implemented, though with more overhead than usual functions) would keep lispers shut about lua just being "another half-implemented lisp derivative" :-P
</lua_vs_lisp_rants>

Dunno. I'd simply use cpp if I needed a macro language for Lua.


as for the syntax for value constraints - prefix or postfix syntax allowing usual lua
expressions would be cool:

function fun(s($==1 or $=="a"), n isnumber($))
	local s2 (isstring($) and #$>5) = somethingcool(s)
	return somethingboring(s,s2,n) isstring($)
end

or something similiar.

function fun( s:a_or_1, n:number ) :string
	local s2 :string_longer(5) = somethingcool(s)
	return somethingboring(s,s2,n)
end

This of course only shows it 'can be done'. No true Lua functions but yes, 'range(1,10)' --which is built-in-- and 'string_longer(5)' -- which is not-- are both meaningful and can be done. Actually, they're done.

Return type is best to be before body, since there may be multiple return statements (which all should adhere to the same constraints). Ugh. ;)


the idea is to make it as flexible as asserts(), yet
in-place, not to mention use of existing mechanisms in lua, rather
than introducing new ones.

personally i find value constraints a bit redundant (type constraints
would be subset of value constraints in lua), useful only for the
debugging stage - i'm the kind of keep-it-short type of person
(and when it breaks ... well then). if something may break
seriously, that's what pcall() is for.

It's all about "marking the edges of the road", a very useful thing in these dark Finnish evenings and mornings; perhaps that's why I am bringing this up...? :P In countries with more sunlight, you might see type bugs even without?? :)


by the way, will be the current constraints code available on
the svn soon? still rather cool patch to take a look/steal/ bastardize :)

That's what I'm asking. As to my habits, you need svn, then svn co svn://slugak.dyndns.org/public/lua-jacket/

If nothing more, please 'svn log' and 'svn update' to see my remarks and daily progress..... Is this exhibitionism, or what?!!

-asko



On Mon, Oct 23, 2006 at 09:45:43AM +0200, askok@dnainternet.net wrote:

I'm repeating the request for help, from people interested
in getting function parameter, local var, and return value
constraints to Lua.

The approach I'm taking is a patch, because:
   - I'd like this feature to get _into_ Lua, instead of
being a kludge add-on
   - performance should be made as good as possible

Current state of the patch is that all parsing is done
(and works); constraint strings are placed in variable and
function data structures in seemingly meaningful places.

What remains is ways to enforce the constraints at
runtime.  A group of people for discussing different
alternatives would be useful, as bouncing something within
a sole person's head is never the most creative approach.

We could use this list, but I'm afraid it's bound to get
into teeny-weeny details pretty soon (= instantly).  Once
a working, tested concept patch is there, the list can be
informed.

thanks,   :)
-asko