lua-users home
lua-l archive

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




On 2017-06-19 06:49 PM, Italo Maia wrote:
(1) '5' + 5 gives you an 10.0 in the interactive console. Isn't this a weakly typed language behavior?

This is called operator overloading, not weak typing. More specifically, operator +, when applied to a string and any number type, results in the conversion of the string to a float, followed by (float) addition.

Take a look at this Rust code, for example:

use std::ops::Add;
struct LuaString<'a>(&'a str);
struct LuaInt(i64);
struct LuaFloat(f64);
impl<'a> Add<LuaInt> for LuaString<'a> {
    type Output = Option<LuaFloat>;
    fn add(self, i: LuaInt) -> Option<LuaFloat> {
        self.0.parse::<f64>().ok().map(|x| LuaFloat(x + (i.0 as f64)))
    }
}
fn main() {
(LuaString("5") + LuaInt(5)).map_or_else(|| println!("nil"), |x| println!("{}", x.0))
}

(run it on your browser: https://play.rust-lang.org/?gist=1f4e2f3b2486870d628110eafd4c73e6&version=stable&backtrace=2 )

It takes a LuaString and a LuaInt and turns them into a LuaFloat, yet it's *strongly* typed AND *statically* typed. (note: Rust doesn't add ".0" when printing floats like Lua does. but it's still a float.)

(2) '5' + 5 > 10.0, string plus integer equals float. If an error was throw here, it would be easier to understand than the given behavior.

With stronger typing, you still wouldn't get an error.



2017-06-19 17:35 GMT-03:00 Coda Highland <chighland@gmail.com <mailto:chighland@gmail.com>>:

    On Mon, Jun 19, 2017 at 2:15 PM, Italo Maia <italo.maia@gmail.com
    <mailto:italo.maia@gmail.com>> wrote:
    > Well, out of the features mentioned, strong typing would be a
    simplification
    > of the language, by all means. Anyway ... a roadmap would be nice.

    Two things:

    (1) Lua is already strongly typed. It just has a very small list of
    recognized types, and one of those types is "table".

    (2) Stronger typing than this would not simplify the language
    whatsoever. It would require a mechanism by which user-defined types
    can be added as well as mechanisms by which these types can be
    identified, manipulated, and instantiated. You could reasonably argue
    that it might make the language more ROBUST, but it would not by any
    means be SIMPLER.

    /s/ Adam




--
"A arrogância é a arma dos fracos."

===========================
Me. Italo Moreira Campelo Maia
Co-fundador do Grupo de Usuários Python do Ceará
Secretário ForHacker (fb.com/ForHackerSpace <http://fb.com/ForHackerSpace>)
Desenvolvedor Full-Stack, Escritor, Empresário, Visionário
-----------------------------------------------------
Meu Livro <http://bit.ly/flask-amazon>, Site <http://www.italomaia.com/>, Blog <http://eusouolobomau.blogspot.com/>
===========================

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.