On adding type information to a dynamically typed language

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

On adding type information to a dynamically typed language

Jorge Visca
"To type or not to type: quantifying detectable bugs in JavaScript"

https://blog.acolyer.org/2017/09/19/to-type-or-not-to-type-quantifying-detectable-bugs-in-javascript/


Haven't read it very deeply, but seems of interest.

As a curiosity, the first example of bug wouldn't be possible in Lua,
because it's better designed.


Jorge


Reply | Threaded
Open this post in threaded view
|

Re: On adding type information to a dynamically typed language

Dirk Laurie-2
A patch that makes a parameter list
   (x:number)
syntactic sugar for
   (x) assert(type(x)=='number')
would be easy, has anyone done it already?

I know about MetaLua and Ravi, I don't mean anything like those,
I mean just a little patch.


2017-09-20 4:10 GMT+02:00 Jorge <[hidden email]>:

> "To type or not to type: quantifying detectable bugs in JavaScript"
>
> https://blog.acolyer.org/2017/09/19/to-type-or-not-to-type-quantifying-detectable-bugs-in-javascript/
>
>
> Haven't read it very deeply, but seems of interest.
>
> As a curiosity, the first example of bug wouldn't be possible in Lua,
> because it's better designed.
>
>
> Jorge
>
>

Reply | Threaded
Open this post in threaded view
|

Re: On adding type information to a dynamically typed language

Gavin Wraith
In message <CABcj=t=[hidden email]>
          Dirk Laurie <[hidden email]> wrote:

> A patch that makes a parameter list
>   (x:number)
> syntactic sugar for
>   (x) assert(type(x)=='number')
> would be easy, has anyone done it already?

I sometimes use

 the = function (TYPE, VALUE)
       assert (type (VALUE) == TYPE)
       return VALUE
       end

OK, a statement like
   f (the ("number", x), the ("string", s))
is more clumsy than f ((x:number), (s:string)).

Another alternative is to do

  is = setmetatable ({ }, {
       __index = function (_, TYPE)
                 return function (VALUE)
                 assert (type (VALUE) == TYPE)
                 return VALUE
                 end end })

and use
   f (is.number (x), is.string (s))
which looks a lot better, without any patching.
--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: On adding type information to a dynamically typed language

Luiz Henrique de Figueiredo
>   is = setmetatable ({ }, {
>        __index = function (_, TYPE)
>                  return function (VALUE)
>                  assert (type (VALUE) == TYPE)
>                  return VALUE
>                  end end })
>
> and use
>    f (is.number (x), is.string (s))
> which looks a lot better, without any patching.

This is nice but creates lots of functions. Here is an alternative:

local types = {
"nil", "boolean", "number", "string", "table", "function", "userdata", "thread",
}

is = {}

for _,t in pairs(types) do
        is[t] = function (v)
                        if type(v)~=t then
                                error(t.." expected, got "..type(v),2)
                        end
                        return v
                end
end

function f() end
x=2
s="a"
s=2
f (is.number (x), is.string (s))

Reply | Threaded
Open this post in threaded view
|

Re: On adding type information to a dynamically typed language

Ulrich Schmidt


Am 20.09.2017 um 13:28 schrieb Luiz Henrique de Figueiredo:

>
> local types = {
> "nil", "boolean", "number", "string", "table", "function", "userdata", "thread",
> }
>
> is = {}
>
> for _,t in pairs(types) do
> is[t] = function (v)
> if type(v)~=t then
> error(t.." expected, got "..type(v),2)
> end
> return v
> end
> end
>
> function f() end
> x=2
> s="a"
> s=2
> f (is.number (x), is.string (s))
>

I like it! But i still i dont like any prefix solution / funtion call
for this task. I would like to write in my sources something like:
   f(x.string(), y.number())
or better:
   f(x.string, y.number)
hence we have to modify the metatables for all data types. Each
metatable needs only 1 method: string() for string metatable, number()
for number metatable and so on. This function should return the value
itself. If i call for instance x.string() on a number value, the method
is undefined and a error is raised.

May be i dont see the drawbacks of this idea?

Ulrich.


Reply | Threaded
Open this post in threaded view
|

Re: On adding type information to a dynamically typed language

Ulrich Schmidt
Forget about my last post .... stupid idea ;)

Am 22.09.2017 um 08:59 schrieb Ulrich Schmidt:

>
> I like it! But i still i dont like any prefix solution / funtion call
> for this task. I would like to write in my sources something like:
>    f(x.string(), y.number())
> or better:
>    f(x.string, y.number)
> hence we have to modify the metatables for all data types. Each
> metatable needs only 1 method: string() for string metatable, number()
> for number metatable and so on. This function should return the value
> itself. If i call for instance x.string() on a number value, the method
> is undefined and a error is raised.
>
> May be i dont see the drawbacks of this idea?
>
> Ulrich.
>
>