objeck / objeck-lang

Objeck is a modern object-oriented programming language with functional features tailored for machine learning. It emphasizes expression, simplicity, portability, and scalability. The programming environment consists of a compiler, virtual machine, REPL shell, and command line debugger with IDE plugins.
https://objeck.org
Other
157 stars 11 forks source link

[discussion] Procedural way of calling math functions #271

Closed ghost closed 1 year ago

ghost commented 1 year ago

I think it's really convenient and could make it easier for procedural programmers to embrace Objeck. For example, instead of 2.0->Sqrt() you could write Sqrt(2.0) like on C. Please note that this is only limited to math functions. My idea is that this could be implemented in an external library (not part of the language) that the users could link with to provide a familiar way of calling math functions. The library could be named cmath.

To be more clear, something like this:

#~~
# Use a C-like procedural syntax for math functions
~~#
bundle cmath {
        class cmath {
                function : Sqrt(a: Byte) ~ Byte {
                        return a->Sqrt();
                }
                function : Sqrt(a: Int) ~ Int {
                        return a->Sqrt();
                }
                function : Sqrt(a: Float) ~ Float {
                        return a->Sqrt();
                }
        }
}

Please note that this is only my idea. I didn't test the code to know if it worked or not. The reason there are three overloaded versions of Sqrt is for Sqrt to work with any numeric value, regardless of whether it's a byte, integer, or floating point, as it always returns the result in the corresponding type. I don't know if there is any better solution to implement this or not.

Update: Yeah I know we could write Float->Sqrt(2.0) but this is too wordy IMO.

Update: If you think the users could confuse 2.0->Sqrt() with Sqrt(2.0) then I suggest the functions on the cmath module should have all lowercase names like on C.

Update: Well, it seems my idea doesn't work. I still have to use cmath->sqrt(2.0) and this is not any better than Float->Sqrt(2.0). The C programmers will have to accept that they are learning a new language with a completely different syntax and get used to it.

objeck commented 1 year ago

Did you try Int->Sqrt(16)->PrintLine(). Procedural functions are backed by primitive methods.