No kotlin.Math class in kotlin 1.2 as it is said in the documentation - kotlin

I have been dealing with kotlin multiplatform alot recently, and I totaly understand the nature of the development. Initially, I had my own expected Math class (in a common module) and I had actual classes in the JS and JVM environment.
Since I like to read the documentations, I found that the Math liblary has been added to the standard liblary since kotlin 1.2. this trouble me as I am using kotlin 1.2.51 and I get an error trying to access the class from kotlin.Math in my common module and any of my platform specific module.
What am I not geting? How do I get access to the kotlin.Math class in my common module?

The Math-class is deprecated and the deprecated-message contains:
Use top-level functions from kotlin.math package instead.
(see also https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-math/index.html)
So somehow the answer of #mTak is right, even though it was not mentioned, that you should use the kotlin.math.*-import instead of your Math-class.
Alternatively, you can also import kotlin.math.max, etc. depending on which function you actually require.
The more I think of it: I don't know whether there ever was a Math-class in the jvm-variant of Kotlin (couldn't find anything regarding it) and so in a multiplatform project the Math-class-access probably should always have failed.

Import it like this: import kotlin.math.*

In the Kotlin standard library math functions are provided as top-level functions in the kotlin.math package.
Therefore you need to import that package and then you'll be able to use functions from it, like sin, sqrt and so on.
import kotlin.math.*
val sqrt2 = sqrt(2.0)
You can also import functions one by one, e.g. import kotlin.math.sqrt or even call them fully qualified val result = kotlin.math.sqrt(2.0)

After a while (I even feel stupid). I found that the kotlin.math library in kotlin common modules was already added. The only difference was, it had no the 'Math.' predecessor as I am normally used to.
So, Math.round(x: Float) was just round(x: Float)
Math.sin(x: Float) was just sin(x: Float)

Related

Where is the PyQt5 documentation for classes, methods and modules?

I'm trying to learn PyQt5 and I am finding it very difficult since I can't just guess what methods are available. I've just spent an entire week trying to find a method to simulate a button push. I eventually found the solution ( QPushButton.animateClick() ) only after stumbling across an example someone left out there (how did this person know this?). It's very difficult to develop without some reference to what's available for tools!
Riverbank has a version of what I'm looking for but it is not complete making it virtually useless.
pyqt5 being a qt5 binding has almost all the functionalities (there are minimal known incompatibilities) so the qt5 documentation: https://doc.qt.io/ is valid for pyqt5 except for small exceptions.
Although the target of the documentation is c++ the description of the classes and methods are generic, so they also validly apply for pyqt5, on the other hand many of the examples are written in c++ but the translation to python in many cases is trivial .
So to avoid doing a double task it seems that Riverbank Computing Limited only documents the exceptions indicated in the pyqt5 docs: https://www.riverbankcomputing.com/static/Docs/PyQt5/
The next part of my answer will propose tips to handle the Qt documentation.
The Qt documentation also has an easy to understand structure, for example let's analyze the QPushButton class (https://doc.qt.io/qt-5/qpushbutton.html):
At the top there is a table:
This table indicates how to include the class in a C++ project, how to add it to qmake, from which class it inherits, and which classes inherit from it. From the above, relevant information for PyQt5 can be extracted, such as to which sub-module the class belongs to: In this case we use QT += widgets that inform us that it belongs to the QtWidgets sub-module, in general if Qt += submodulefoo belongs to QtSubModuleFoo (camelcase)
If you want to know all the methods of the QPushButton class, you must use the "List of all members, including inherited members" link below the table, in this case the link will be https://doc.qt.io/qt-5/qpushbutton-members.html where is the complete list of all class methods, enumerations, etc.
Other tips to understand the conversion between Qt/C++ and PyQt5/Python are:
Some methods use pointers to receive information such as:
void QLayout::getContentsMargins(int *left, int *top, int *right, int *bottom) const
bool QProcess::startDetached(qint64 *pid = nullptr), etc
those transformed to PyQt5 as:
lay = QtWidgets.QXLayout()
left, top, right, bottom = lay.getContentsMargins()
process = QProcess()
# ...
ok, pid = process.startDetached()
Some methods collide with reserved words such as exec , raise, print, etc so to avoid incompatibilities, the underscore is added at the end: exec_, raise_, print_, etc
In Qt, the Q_SLOT and Q_SIGNAL that are translated into python are used through the #pyqtSlot and #pyqtSignal decorators.
In conclusion, my recommendation is that you use the Qt and PyQt5 documentation at the same time to know all the functionalities, in addition there are many Q&A in SO about translations from one language to another so you could learn from there.
The Qt documentation can also be consulted using the Qt Assistant tool.
The main PyQt5 documentation is on the official website:
https://www.riverbankcomputing.com/static/Docs/PyQt5/
But it's still incomplete, and most parts refer to the official Qt documentation:
https://doc.qt.io/qt-5/
While that's C++ oriented, consider that almost every module, class and function behave exactly in the same way as it does in python, so it's usually better to use that.
Consider that:
in the function lists you'll always see the returned type on the left of each function;
"void" means that the function returns None;
when overriding some existing method (expecially private and virtual), you always have to return the expected types listed for that function;
function arguments are usually written in the form [const] Type argName=default: you can usually ignore the "const" part (it's a C++ term), while the argName for keyword arguments might be different in PyQt;
some functions could have different names, since they're reserved on python (print, raise, etc); in those cases, an underscore is always appended;
some positional or keyword arguments might be different, or the return type signature might; that's because in C++ you can use a pointer to a variable as an argument, and the function will change that variable using the pointer (this is an oversimplification);
all "properties" are not python properties, and they are only accessible through their parenthesis functions, such as self.width() an self.setWidth();
some methods have different overrides, in some cases python has special cases with different arguments that are not available in C++, and viceversa; also, some methods don't exist at all in one case or the other;
My suggestion is to always use the official documentation, it's only a matter of habit to get used to the C++ references (and you'll see that it is educational too); whenever some doubt raises, check the PyQt documentation to see what's different and use the help command in the python shell.

Can I use or import the functions in CharJVM.kt file which is inline function collection defined by the Kotlin platform?

I want to make my custom inline function in .kt file using checkRadix function already implemented by Kotlin.
But I cannot import it. How can I import and use it?
I tried
import kotlin.jvm.JvmMultifileClass.*
kotlin.jvm.JvmMultifileClass.checkRadix(radix)
But I can't compile and there is no recommended resolution by IDE.
That function is marked as internal, which means it's only available within that module — i.e. within the Kotlin stdlib, not to your code.
I don't know why it's marked like that; maybe JetBrains consider it an implementation detail.  But they clearly don't want it being used by any other code.
(Of course, it wouldn't be hard to reimplement yourself.)

How to use BigDecimal in Kotlin Multiplatform?

I followed the tutorial https://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html, then I successfully create the folders of androidmain, iosmain and commonmain.
However when I want to implement the datatype BigDecimal in the commonmain. It won't work. I need the decimal dataype for the currency.
I know that the question is old, but, in case anyone stumbles upon this topic, I made a KigDecimal library that implements BigDecimal and BigInteger for kotlin multiplatform (for jvm and js). The library is distributed completely freely. Therefore, I invite everyone to supplement and expand it, if desired.
On the jvm side, BigDecimal and BigInteger are just the corresponding types from java. And on the js side is used https://www.npmjs.com/package/bigdecimal.
The main repository is located here: https://gitflic.ru/project/mikhaylutsyury/kig-decimal
There is also a mirror on github: https://github.com/YuryMikhailuts/kig-decimal
But the mirror can sometimes lag a little behind the main repository.
There is no support for BigDecimal in the Kotlin common code (yet).
You may have a look at the related thread
https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631
You may create your own implementation for such a class with expect and actual keywords.
https://kotlinlang.org/docs/reference/platform-specific-declarations.html
The idea is as follows:
you declare expect declarations for the BigDecimal type in common code
you use the actual annotations at every platform to supply the platform specific implementation (e.g. JVM's BigDecimal class)

Can I include a single ObjC class when compiling Swift code?

In my understanding, it will be possible to work with Obj-C classes from Swift and write test cases much more quickly, and see the results in a playground project.
Does *.playground support including just a single class, like an .m/.h pair?
How does it work? Do I need to compile this class separately, or is it done automatically?
Unfortunately, a pure playground allows to import only Cocoa framework (for now, at least).
If you want to import other modules, you need to create a playground file inside an existing project. That way, the underlying Swift code inside the playground can access your symbols.
Reference: Does swift playground support UIKit?

Rebol, extensions and function naming

I am working on some extensions for Rebol 3 (posix/fann/math).
To avoid global namespace pollution, I am exporting the functions with a simple prefix source identifier. For example: POSIX-FORK for fork, or POSIX-NANOSLEEP for nanosleep.
Is there any better approach or official Rebol naming convention?
That's a pretty standard naming convention for Rebol exports, though they should be lowercase in the code of course. The all uppercase thing is just a naming convention when referring to functions in chat clients or web sites that can't show code like this. You generally don't uppercase any words in Rebol code unless they are used for something else.
However, if you want to avoid global namespace pollution, declare your extension module with the options: [private] header. That will make it so the exports of your module are only imported by modules or scripts that request them explicitly with import or the needs header. This especially goes for modules or extensions that export low-level C-like APIs, which are best only imported by the modules that implement the high-level wrappers. It's good to remember that the module part of the extension is a full Rebol module, and it is often best to put your high-level wrapper code there and not export the C-like functions at all, keeping them for internal use.
An additional trick is that when you are exporting constants or enum values, it is best to put them in an object in your module and export the object instead. That way you don't export to the global namespace and you can protect the words from modification.
Another trick is to not export stuff at all and have people import your module using the import function. Unless you mark your module's words as hidden they will still be available even if they're not exported. This is a little inconvenient in most cases though, so it's better to use a private module instead. You can also export your high-level API and not export your low-level API, so the low-level API is available to import if someone wants to use it.
Check here for a more thorough answer about how modules and extensions are used: How are words bound within a Rebol module?