Let's say I have a higher order function, which registers some sort of a click listener. I can document its purpose and the listener parameter that's passed in like so:
/**
* Adds a [listener] that's called when the item is clicked.
*
* #param listener The listener to add
*/
fun addClickListener(listener: (count: Int) -> Unit) {
...
}
My question is, is there a way to document the parameters of the listener? This would be count in this example. In my actual use case, I have multiple parameters in my listener.
What I've noticed is that the [listener] text is clickable in the documentation view where I'm using this function, but it just shows an empty dialog about it. Is there a way to describe the parameters there somehow?
For now, I've ended up describing the parameters of the listener with #param blocks at the addClickListener method, but this results in a warning in the IDE, and I'm wondering if there's a more proper way to do it.
As of Kotlin 1.1 there is no syntax for documenting the parameters or return value of a function type used as a function parameter. There is an open YouTrack issue covering this.
Related
I want to call some api in the background every X minutes and then process the json file I get
I've lokked into this documentation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/java.util.-timer/schedule.html
I'm new to kotlin (I used java before) and I have no idea how to use those functions, any examples of usage would be helpful.
Right now I have something like this:
Timer("NameOfMyTimer", true).schedule(refreshImages(knownPosts, knownFiles, httpClient), TimeUnit.MINUTES.toMillis(5))
And the result is:
None of the following functions can be called with the arguments supplied:
public open fun schedule(p0: TimerTask!, p1: Date!): Unit defined in java.util.Timer
public open fun schedule(p0: TimerTask!, p1: Long): Unit defined in java.util.Timer
What did I wrong? How should I call those functions?
I thought that I'm supposed to pass my function "refreshImages" to the timer with list of arguments it should be called with...?
I think I just don't get the "function is object" philosophy right.
You're trying to call
.schedule(refreshImages(knownPosts, knownFiles, httpClient), TimeUnit.MINUTES.toMillis(5))
So you're passing as first argument the result of refreshImages(knownPosts, knownFiles, httpClient), and as second argument a number of milliseconds.
And as you can see from the compilation error, the Timer class has two schedule() methods, but both expect a TimerTask as argument. And your refreshImages method doesn't return a TimerTask, so that doesn't compile.
If you want to use one of these two Timer methods, you need to create an instance of TimerTask, and pass that as argument.
My guess is that you would like to pass a function that will be executed after some delay. That's not what you're doing right now. What you're doing is that you execute refreshImages() immediately, and pass its returned value to schedule().
Passing a function is not possible with the native Timer schedule method: it doesn't expect a function, but a TimerTask. But as the Kotlin documentation you linked to shows, it's possible by calling one of the extension functions of the Kotlin standard library.
The signature of the schedule extension function is
inline fun Timer.schedule(
delay: Long,
crossinline action: TimerTask.() -> Unit
): TimerTask
So, as you can see, its first argument is a delay, and its second argument is a function with TimerTaskas receiver. So you can call this extension function using a delay as first argument, and a lambda as second argment:
timer.schedule(TimeUnit.MINUTES.toMillis(5)) {
refreshImages(knownPosts, knownFiles, httpClient)
}
Is it possible to access arguments/parameters passed to an emitted event within an inline / in-template handler? Something like:
<component #some-event="someObject.field = $arguments[0]"></component
What I'm trying to do exactly is assign a value to an object in the scope. I know I can create a method to do that and use it as a handler for the event but I was wondering if this could work as an inline statement.
It is not $arguments[0], but just arguments[0] (without the $). I am surprised that it actually works in the inline handler. So the following code is valid and will work:
<component #some-event="someObject.field = arguments[0]"></component>
The docs for Methods in Inline Handlers specifies $event as a special variable that gets the first parameter passed via event. I have always used it till now.
After reading your question, a bit more research led me to this reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
It seems every javascript function has a local variable called arguments, which is used when a function is expected to get variable number of arguments.
Using arguments[] as inline statements is definitely possible, but not documented anywhere in the context of vue.js framework. On the other hand, if you use $event in inline handler for events, it feels safer as it is documented clearly and will not break in a future version of Vue.js
Sample usage for $event:
<component #some-event="someObject.field = $event"></component>
There is something that not many people know.
Inline handler besides the $event has access to arguments.
For example I was using v-dropzone which is passing many arguments (e.g. file, response).
By using
<v-dropzone #some-event="callMethod($event, 'some other arg')" >
will catch only the first argument.
So the solution was:
<v-dropzone #some-event="callMethod(arguments, 'some other arg')" >
I use a lot of callbacks and I want the editor to detect the type of function callback that is expected by the method, with a specific nr of parameters of specific types.
Right now it just says Function and it can be anything but I want to make it specific, this in IntelliJ.
Intellij (and the family of editors) support the Google Closure Compiler syntax.
So you'd do something like this:
/**
* #param {function(Number, String)} bar
*/
function foo(bar) {};
Note the lowercase f in function.
when I type a method call into intellij it doesn't suggest parameters, is it possible to make it suggest parameters?
for example calling
void setFoo( Foo foo );
in context
Foo foo = new Foo();
dto.setFoof(...
could then either prepopulate or suggest foo (netbeans does this).
Also parameters, like boolean could suggest, true or false.
Is it possible to make it suggest them, without additional typing?
You can press CTRL+J (Parameter Info) to get a tooltip showing an abstract of all parameters for all overloaded variants of a method (place the caret on the method). When using code completion (CTRL+Space) or SmartType (CTRL+Shift+Space) inside the method's parentheses, IntelliJ tries to intelligently propose parameter values that fit in the current context (such as true or false or a method yielding true or false for a boolean parameter).
In the Settings dialog (Settings... -> Editor -> Code Completion) you can configure the Parameter Info feature by activating an autopopup or enabling showing full signatures.
First off, I am using a version of older version of dojo, so dojo.event.connect is the proper syntax. My question is this: How do I access the event in the function that I call when the event is fired.
Basically, Ii am dynamically creating a button and then connecting an event on "onClick"
var _btn = dojo.widget.createWidget(widget parameters);
dojo.event.connect(_btn,"onClick","myFunction");
In myFunction, I need to be able to access the attributes of _btn. I have tried passing _btn as the context of dojo.event.connect but this doesn't work. It also wont pass _btn as a parameter for myFunction when I try that. Is it possible to either A) somehow pass _btn as a parameter into myFunction or B) Access the event that is fired in myFunction when _btn is clicked.
I don't know if there is a more convenient way to solve your problem, but in the worst case, you could just use a closure as a surefire way to pass the parameter:
change myfunction from
myfunction(arg1, arg2){
into
myfunction(btn, arg1, arg2){
//using btn here
and use dojo.partial (or dojo.hitch) to create a function that always reveives a certain button as a parameter (and then pass it to the connect):
dojo.event.connect(_btn, 'onClick', dojo.partial(myFunction, _btn));