Determine version of Kotlin at runtime - reflection

What can be written to determine the version of Kotlin at runtime?
fun main(args : Array<String>) {
println("v" + System.getProperty("java.version"))
}
prints a version, but it is the Java JDK version. Using "kotlin.version" prints null. Can this be done at runtime?
If the general answer is no, is there a way to embed this information from the compile phase into a particular function or class?

Use kotlin.KotlinVersion.CURRENT:
fun main(args: Array<String>) {
println(KotlinVersion.CURRENT)
}
Try it out here by changing the version in the bottom-right corner of the page.

Related

Can't infer proper type myself in Kotlin when using JavaFX

I just started to study Kotlin and JavaFX by following tutorial.
I could see blank JavaFX windows, and I proceed next step that using FXML.
import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.Scene
import javafx.stage.Stage
class AppMain : Application() {
override fun start(primaryStage: Stage) {
primaryStage.title = "Try JavaFX"
val fxml = javaClass.getResource("fxml/Main.fxml")
val root = FXMLLoader.load(fxml) // ERRORS here! `load`
val scene = Scene(root)
primaryStage.scene = scene
primaryStage.show()
}
}
However, I couldn't figure out that how to avoid the type inferencing error like:
Error:(12, 31) Kotlin: Type inference failed: Not enough information to infer parameter T in fun <T : Any!> load(p0: URL!): T! Please specify it explicitly.
From the message, I understand that I have to write the type of variable fxml explicitly.
But I have no idea that what type should be labeled to fxml.
I tried to read the document about JavaFX, but I couldn't figure it out.(I'm not familiar with Java and Kotlin)
I tried to type like URL but it does not make sense.
Many JavaFX & Kotlin example codes that I could find from google does not seems to have the problem like this. (Are the example codes written in previous version?)
What type should I put for the variable?
Or did I miss something other?
Environment and Codes
Environment
JDK 11
JavaFX 11
Kotlin 1.2.71
My complete trial code
https://github.com/QuietJoon/StudyKotlin-JavaFX/tree/fxml
The problem isn't the parameter to the FXMLLoader.load function (which is a java.net.URL object, as returned by javaClass.getResource). It's that this function returns a generic type:
public static <T> T load(URL location)
The Kotlin compiler needs to know what type your root variable will be (as you've not explicitly defined it), but it can't know that as there's nothing in the code that will allow it to infer this.
A quick Google returned this example which has this code in it (in Java):
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));
As you can see here, the root variable is of type Parent. So what you need to do is provide this type (i.e. what you expect the load function to return) in some way. Here are two different ways you could do this:
Specify the type explicitly when declaring the variable: val root: Parent = FXMLLoader.load(fxml)
Specify the generic type when calling the method: val root = FXMLLoader.load<Parent>(fxml)
Note also that in your build.gradle file in your github repo there's a mistake that means the code didn't immediately compile when I fetched it:
compile "org.openjfx.javafx.fxml:11:$platform" should be compile "org.openjfx:javafx-fxml:11:$platform" (one of the dots should be a colon).

What is the difference between find and firstOrNull?

Given the following code extracted from Kotlin Koans:
fun Shop.findAnyCustomerFrom(city: City): Customer? {
// Return a customer who lives in the given city, or null if there is none
return customers.firstOrNull { it.isFrom(city) }
}
My own solution used customers.find. Both work in the koan scenario.
The documentation for firstOrNull and find seem to be very similar.
What is the difference between these two functions?
In this thread from 2014, Kotlin community members and JetBrains staff discuss the merits of the different methods find and firstOrNull:
https://youtrack.jetbrains.com/issue/KT-5185
While not an official statement, JetBrains' employee Ilya Ryzhenkov describes it as:
I think we can undeprecate find and make it an alias to firstOrNull. Much like indexOf has well-known semantics, find is also widely recognised as "find first item matching predicate or return null if nothing is found". People who like precise meaning can use firstOrNull, singleOrNull to express the intent.
In other words:
find(predicate) and firstOrNull(predicate) are identical in behaviour and find can be considered alias of firstOrNull
find is kept around as an alias because it's more intuitive and discoverable for programmers not already familiar with these Linq-style - or functional - methods.
In actuality the definition of Array<out T>.find is not defined as an alias, but as a wrapper (though the optimizing compiler will inline it, effectively making it an alias):
https://github.com/JetBrains/kotlin/blob/1.1.3/libraries/stdlib/src/generated/_Arrays.kt#L657
#kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.find(predicate: (T) -> Boolean): T? {
return firstOrNull(predicate)
}
Ditto for Sequence<T>.find:
https://github.com/JetBrains/kotlin/blob/1.1.3/libraries/stdlib/src/generated/_Sequences.kt#L74
#kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.find(predicate: (T) -> Boolean): T? {
return firstOrNull(predicate)
}
(I'm not a Kotlin user myself, but I'm surprised that these methods are implemented as compile-time generated code manually defined for each collection type instead of as a single JVM generic method - is there some reason for this?)

Swift 3.0: Type 'RLMResults<RLMObjectType>' does not conform to protocol 'Sequence'

I'm seeing:
Type RLMResults<RLMObjectType> does not conform to protocol Sequence
when converting to Swift 3. I am looking for a version of Realm for Swift 3.0, however I can't find a proper one, even a branch.
I tried to write an extension, but still the same error message:
extension RLMResults: Sequence
{
public func makeIterator() -> RLMResults.Iterator
{
return NSFastEnumerationIterator(self)
}
}
How can I fix it, or where I can find the proper framework or branch?
Update:
The code that needs Sequence protocol:
public class RealmChartUtils: NSObject
{
/// Transforms the given Realm-ResultSet into an xValue array, using the specified xValueField
public static func toXVals(results: RLMResults<RLMObject>, xValueField: String) -> [String]
{
let addedValues = NSMutableSet()
var xVals = [String]()
for object in results // <-- here needs Sequence protocol
{
let xVal = (object as! RLMObject)[xValueField] as! String!
if !addedValues.contains(xVal!)
{
addedValues.add(xVal!)
xVals.append(xVal!)
}
}
return xVals
}
}
So in swift 2.2, we added an extension like below right after above function:
extension RLMResults: SequenceType
{
public func generate() -> NSFastGenerator
{
return NSFastGenerator(self)
}
}
extension RLMArray: SequenceType
{
public func generate() -> NSFastGenerator
{
return NSFastGenerator(self)
}
}
So in Xcode 8 beta 3, it says SequenceType is renamed to Sequence, however this is the original question because I tried to make above extension conforms to Sequence (which is my first posted extension), the compiler keeps saying Type RLMResults<RLMObjectType> does not conform to protocol Sequence
Could I solve this without upgrading Realm swift framework (I would rather wait a release, not self building the master branch at this moment)
How can I get RLMResults to conform to Sequence?
See Using Realm Objective-C from Swift for information on how to make Realm Objective-C's types work more naturally from Swift. In particular, take note of the section on RLMSupport.swift:
We recommend you compile the Swift/RLMSupport.swift file (which is also available in our release zip).
This file adds SequenceType conformance to Realm Objective‑C collection types and re-exposes Objective‑C methods that aren’t natively accessible from Swift like methods including variadic arguments.
Realm Objective‑C doesn’t include this file by default because that would force all users of Realm Objective‑C to include the hefty Swift dynamic libraries regardless of whether or not they use Swift in their app!
However, due to a bug in the Swift 3 compiler the conformance of RLMResults and RLMArray to Sequence has been temporarily disabled. Hopefully the Swift compiler issue will be resolved prior to Xcode 8 moving out of beta.
Where I can find Realm with support for Swift 3?
Support for Swift 3 with Realm Swift is present on Realm's master branch, alongside the existing support for Swift 2. It will become available in releases of Realm Swift once Xcode 8 and Swift 3 are finalized, later this year.

QtJambi won't construct a QApplication - Scala

I am impatient enough not to like reading books or tutorials on the stuff i want to learn. That said, i almost always will get the toolchains ready and start firing code off with whatever crazy idea gets on my head.
Scala piqued my interest today and i inmediately setup IDEA with the Scala plugin to get started... now, i got some knowledge about the syntax and why Scala has that much amount of Awesome-Sauce, so i decided to test it out with another technology i didn't know: QT, especially QtJambi. I imported the QtJambi dist into the IDEA project structure and wrote this snippet:
import com.trolltech.qt.gui._
class MyMainWindow extends QWidget {
def showWindow = {
setWindowTitle("Scala Jambi Test")
resize(250, 250)
move(300, 300)
show()
}
}
object MainApp extends QWidget() {
def main(args: Array[String]) {
QApplication.initialize(args)
new MyMainWindow().showWindow
QApplication.exec
}
}
It compiles and runs but i get this on the console:
QWidget: Must construct a QApplication before a QPaintDevice
Any ideas on what i am doing wrong are appreciated.
I might add that the same code on a standard java project with the same libs does work.
The problem is that MainApp is extending QWidget, i removed the extension and the thing worked all out of the blue. If anyone knows why please comment, i'd love to know why that went wrong.

ReferenceError: Error #1065: Variable flash.sensors::Geolocation is not defined

I have a browser application and i want to use the Geolocation class. The problem is that i get that error when i try to use Geolocation.isSupported. I have imported the flash.sensors.Geolocation in the file but still get this error.
Any ideas? Thank you
later edit:
i got that error after i tried something like this:
public static function get isGeolocationSupported():Boolean
{
return Geolocation.isSupported;
}
and called this function.
but if i call directly Geolocation.isSupported i get this error:
VerifyError: Error #1014: Class flash.sensors::Geolocation could not
be found.
This feature is supported only on mobile devices. It is not supported on desktop or AIR for TV devices neither on web applications.
If you are receiving this error when publishing your flash as3 swf it is because you need to declare your class as public. Private classes can not be used as document classes because they are out of the class package and therefore are not a part of the private scope.
The following error is due to not instantiating the Geolocation class
EX: geo = new Geolocation();
ReferenceError: Error #1065: Variable flash.sensors::Geolocation is
not defined
The other error
VerifyError: Error #1014: Class flash.sensors::Geolocation could not
be found.
Is because you did not import the class
I tried:
return (Geolocation != null);
But it gave me the same error - you will need to do a try/catch, unfortunately in this situation.
var result:Boolean = false;
try
{
result = Geolocation.isSupported;
}
catch (e:Error)
{
trace(e.message);
}
finally
{
return result;
}
Maybe my answer come too late, but I was receiving same error yesterday and I could arrange it. You can test the app in Adobe Device Central, where you can change the device you are using. Actually it doesn't work in the default device. I have had also problems when trying to use the app in my smartphone through a swf player, because of the flash version. I had to change it to 10.1 or lower versions (versions supporting Geolocation), and anyway I haven't been able to make it work well, though it was running ok on Device Central.

Resources