Skip to main content
↑
↓
Spherical forums
Community for the
Sphere game engine
New?
Contact Us
to register an account!
1 Hour
1 Day
1 Week
1 Month
Forever
Community
Help
Search
Recent Posts
Log in
Contact Us
News
Spherical
Facebook
-
Twitter
-
Discord chat
New?
Contact us
to register an account!
Creations
Programming
Operator Overloading in JS
1
Print
Topic: Operator Overloading in JS
(Read 4968 times)
previous topic
-
next topic
0 Members and 1 Guest are viewing this topic.
Radnen
Big Chungus
Posts: 1,440
Sphere Studio Developer
Logged
Senior Staff
Wise Warrior
Operator Overloading in JS
October 27, 2015, 12:44:15 pm
Guys, here's something neat I think some of you might find interesting. I was thinking of how I can overload operators in JS and was thinking about using the toString method, but if you use valueOf instead you can add some interesting logic to make operator overloads possible.
Pay attention to how the number 3 is used to figure out which operator was requested in the overload process.
Article:
http://www.2ality.com/2011/12/fake-operator-overloading.html
GitHub Demo:
https://github.com/rauschma/op_overload
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.
Sphere-sfml
here
Sphere Studio editor
here
Fat Cerberus
Big Chungus
Posts: 2,774
*MUNCH*
Logged
Global Moderator
Sphere Developer
Re: Operator Overloading in JS
Reply #1
–
October 27, 2015, 09:18:14 pm
This is a pretty neat trick, although I dislike operator overloading in general because it tends to be a very leaky abstraction. For example, you can't do
"abc" + "def"
in C++, you have to cast at least one of the literals to a std::string.
neoSphere 5.9.2
-
neoSphere
engine -
Cell
compiler -
SSj
debugger
forum thread
|
on GitHub
Flying Jester
Legendary Poster
Posts: 1,161
Logged
TurboSphere Developer
Re: Operator Overloading in JS
Reply #2
–
October 28, 2015, 09:47:03 pm
Operator overloading is also very horrifically abused in C++. The io system in the C++ standard library is one of the worst examples of using operator overloading well, which is very unfortunate since it's most folks introduction to operator overloading in C++.
Nowadays I tend to just write my own io wrapper around C's io streams, using variable templates. It's just nicer to do in every way than C++'s standard library io.
One thing that has been long overdue is the new literal notation, which in C++14 will let you define a string literal as a std::string. Then you could do it without a cast. I like this compromise, since it means a string is just an array of chars normally, but can optionally be a std::string, if that's what you need. It's probably the only part of C++14 (or is it C++17?) I am excited for.
Learn some history?
The Flying Jester's Blog of Sorts
Fat Cerberus
Big Chungus
Posts: 2,774
*MUNCH*
Logged
Global Moderator
Sphere Developer
Re: Operator Overloading in JS
Reply #3
–
October 29, 2015, 05:50:45 pm
I guess the standards committee was trying to keep the language and standard library decoupled. But that kind of thing is exactly what makes C# such a joy to work with--much of what it offers is just compiler magic around some feature or other of the .NET framework. For example a 'using' block is just sugar for calling Dispose at the end (which is valuable--it saves a finally clause), and async/await is a very clean abstraction of threads and promises (or Tasks as .NET calls them). In C++ the STL is completely auxiliary and feels at times more like a warty appendage than a proper compliment to the language.
neoSphere 5.9.2
-
neoSphere
engine -
Cell
compiler -
SSj
debugger
forum thread
|
on GitHub
1
Print
Creations
Programming
Operator Overloading in JS