lua-users home
lua-l archive

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


On Mon, Jun 19, 2017 at 6:04 PM, Italo Maia <italo.maia@gmail.com> wrote:
> Ok ... then, could someone provide an example of a weakly typed language? If
> there is no problem to operate on different types because the operation is
> defined, I'm quite unsure of what could be weakly typed.

PHP is the classic example. In PHP, a string that looks like a number
can have very strange behaviors. For example, 123 == "123foo" because
it coerces the string to a number before comparison. Likewise, "1e3"
== "1000". (Oddly, "123" != "123foo", so it's a little inconsistent.)

C is also a weakly typed language, but because it's also a STATICALLY
typed language the weakness is a little bit less risky. But you can
cast between all pointers freely, so the difference between a string
and a struct is really just a matter of how you choose to look at it.

C++ is somewhat stronger in its typing, because polymorphic data types
carry their own type information with them -- that is, the values
themselves know what their type is, not just because of how you choose
to access them. C++ will LET you violate that but it makes you
intentionally have to try.

Lua is stronger still, because ALL values have their types baked into
them, and it doesn't matter how you go at it, a number is a number, a
string is a string, and a table is a table, and the coercions don't
just treat the same value differently, they create a NEW value of the
new type.

/s/ Adam