|
Perl and Lua have separate arithmetic and concatenation operators 1 + "2"
#3
1 - "2"
#-1
1 . "2" | 1 .. "2"
#"12"
1 . "-2" | 1 .. "-2"
#"1-2"
Python and Ruby throw exception, use explicit type conversion and string interpolation 1 + '2'
Traceback (most recent call last):
TypeError (String can't be coerced into Integer)
1 + '2'.to_i
#3
"1#{'2'}"
#"12"
Hardly any language is perfect, I have not encounter much of operator overload in ruby (nokogiri?) but I believe C++ got it bad.One number type in Lua: > 0 | 0xffffffff
4294967295
> 0 | 0xffffffffffffffff
-1
> 0 | 0x7fffffffffffffff
9223372036854775807
limited but better than JavaScript: 0 | 0xffffffff
//-1
BigInt is an improvement 0n | 0xffffffffn
//4294967295n
0xffffffffffffffffn
18446744073709551615n
it is strict 1n - "2"
1 + 1n
Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
works nice with string interpolation: `${1n}`
"1"
Numbers is a sane example. One can argue it was for good. How about `{} + []`? I believe I can disable this part in JavaScript engine and no one would notice. And misleading `object[key]` where it calls toString, sure I have not tried that in a decade but it is stupid. UTF-16: ""[1] # there were emoji
//"�"
You've said nothing about constructor oriented programming. Unique feature, I have not heard any other language adopted it yet. The post you've replied contents sketch for Ruby. Actually I've got it wrong — every JavaScript function is a closure (Ruby method is not closure) and Prototype method was a class method (not instance method), fixed but ugly: def function(&block)
Class.prototype.new.tap do |c|
c.define_method(:initialize, block)
end.instance_method(:initialize)
end
def function_(object, name, &block)
object.class_eval do
define_method(name, &block)
end
end
Person = function { |name|
@name = name
}
function_(Person.prototype, :name_) {
@name
}
john = new.call Person, 'john'
puts john.__proto__ == Person.prototype
puts john.name_
def function__(object, name, &block)
object.singleton_class.class_eval do
define_method(name, &block)
end
end
function__(john, :name__) {
@name
}
puts john.name__
By the way, you can say "Yes, I know JavaScript has some problems". It is not a secret, everyone knows. |
Lua allows operator overloading with metatables as do ruby and Python with classes
http://lua-users.org/wiki/MetatableEvents
https://docs.python.org/3/reference/datamodel.html#emulating...
https://www.ruby-lang.org/en/documentation/faq/7/
> One number type in Lua:
Not quite true. Lua had only 64-bit floats like JS until version 5.3 and the blazing fast LuaJIT still only has floats. Well, to be honest, it has hidden 32-bit integers for sake of bitwise operations just like JS (well, JS uses 31-bits with a tag bit which is probably a lot faster).
> How about `{} + []`? I believe I can disable this part in JavaScript engine and no one would notice.
That's very simple. {} at the beginning of a line is an empty block rather than an object (yay C). "Disabling" that would break the entire language.
> UTF-16
UCS-2 actually. Back in those days, Unicode was barely a standard and that in name only. Java did/does use UCS-2 and JS for marketing reasons was demanded to look like Java. I don't want to go into this topic, but python, PHP, ruby, C/C++, Java, C#, and so on all have a long history not at all compatible with UTF-8.
> You've said nothing about constructor oriented programming. Unique feature, I have not heard any other language adopted it yet.
I'll give you that JS prototypal inheritance is rather complex due to them trying to pretend it's Java classes. Once again though, the deep parts of both Python and Ruby classes are probably more difficult to explain. Lua's metatables are very easy to understand on the surface, but because there's no standard inheritance baked in, every project has their own slightly different implementation with it's own footguns.
Closures are almost always preferred over classes in modern JS. Likewise, composition is preferred over inheritance and the use of prototype chains while not necessarily code smell, does bear careful consideration.
If someone insists on using deep inheritance techniques, they certainly shouldn't be using class syntax as it adds yet another set of abstractions on top. Object.create() and inheriting from `null` solves a ton of issues.
> By the way, you can say "Yes, I know JavaScript has some problems". It is not a secret, everyone knows.
I'd say if you take the top 20 languages on the tiobe index, it sits in the middle of the pack with regard to warts and weirdness. Maybe people are just attracted to weird languages.