Define type of array on Restler function documentation - restler

When I define an #param as array, or the #return as array, the Explorer that comes with Restler always says array[string]. Is there a way that I can specify what type of array it really is?

Not sure if this works in this case, but have you tried something like int[]? It might generate doc on array[int] (at least that's how PHPStorm handles docblocks on arrays of things).

Related

Why does 'indexOf' not find the specified arrayOfLong?

I have an ArrayList of LongArrays, each of size 2. I am trying to use the built-in 'indexOf' method to find the index of a specific array of longs, and I can't figure out why the code says the array of longs I'm searching for isn't found. See the below screen shot of a debugging session where try to evaluate finding 'longArrayOf(0L,5L)' in the ArrayList. In my mind, longArrayOf(0L,5L) is clearly the first element in the cardHierarchy array. Can the 'indexOf' function not be used for finding arrays? Can anyone suggest an alternate method?
indexOf uses Object.equals() when you pass arrays, which compares by reference address which is different for the LongArray passed to indexOf and the one present in cardHierarchy.
Change it to
cardHierarchy.indexOfFirst { it.contentEquals(longArrayOf(0L, 5L)) }

Why do I have to store the result of a function in a variable?

Is it because some functions will change the object and some don't so you have to store the returned value in a variable? I'm sure there's a better way to ask the question, but I hope that makes sense.
Example case: Why doesn't thisString stay capitalized? What happens to the output of the toUpperCase() function when I call it on thisString? Is there a name for this behavior?
var thisString: String = "this string"
var thatString: String = "that string"
thisString.toUpperCase()
thatString = thatString.toUpperCase()
println(thisString)
println(thatString)
which prints:
this string
THAT STRING
By convention if a function starts with the word to or a past participle, it always returns a new object and does not mutate the object it's called on. But that's not exclusively true. Functions that begin with a verb may or may not mutate the object, so you have to check the documentation to know for sure.
A mutable object might still have functions that return new objects. You have to check the documentation for the function you call.
For a function that returns a new object, if you don't do anything with the returned result or store it in a variable, it is lost to the garbage collector and you can never retrieve it.
String is an immutable class, so none of the functions you call on it will ever modify the original object. Immutable classes are generally less error-prone to work with because you can't accidentally modify an instance that's still being used somewhere else.
All the primitives are also immutable. If all the properties of a class are read-only vals and all the class types they reference are also immutable classes, then the class is immutable.
If you want an mutable alternative to String, you can use StringBuilder, StringBuffer, CharArray, or MutableList<Char>, depending on your needs. They all have different pros and cons.
Why doesn't thisString stay capitalized?
Because that's how the function was coded (emphasis mine):
"Returns a copy of this string converted to upper case using the rules of the default locale."
What happens to the output of the toUpperCase() function when I call it on thisString?
Nothing. If you don't assign it to a variable (save a reference to it) it's discarded.
Is there a name for this behavior?
AFAIK, this is simply "ignoring the return value".
Hope that helps.

Velocity template - retrieving hashmap values

I have a HashMap< String, List> which I fill inside the Java class. When I try to print it out in the Velocity template, it looks fine.
$!valuesMap ##gives {33=[texxxxt], 34=[2019-03-31], 35=[admin], 37=[P1], 40=[value1, value2]}
When I try to access the values directly, it also looks fine.
$!valuesMap.get("40") ##gives [value1, value2]
Problem arises when I try to use a dynamic variable to access the map. I have a list of objects over which I iterate, and each of these objects has an ID. However I cant figure out how to retrieve the value from the map using this ID.
#foreach( $field in $fields )
$!field.ID ##gives the id of the object, i.e. 40
##I would assume this would give me [value1, value2] when ID is 40, but it returns nothing
$!valuesMap.get($!field.ID)
#end
I have tried assigning the ID to a new variable (variable itself prints out fine, but again when I try to access the map, I get nothing). I have tried the notation suggested here and nothing ever prints out, it is honestly driving me up the wall, because I am probably missing something very simple, but cant figure out what it is.
Velocity Engine 1.7 does not convert method arguments towards expected types. So if $field.ID is a number, you have to enclose it in double quotes to get a string:
$valuesMap.get("$field.ID")
Otherwise, the engine simply doesn't find a proper method to call.
Starting with 2.0, Velocity Engine will automatically convert method arguments towards expected types, and your code will work as expected.

Understanding Solr charTermAttr methods

I can across a copy methods from charTermAttr from the org.apache.lucene.analysis.tokenattributes.CharTermAttribute library.
Can anyone explain what copyBuffer and buffer does for charTermAttr? The documentation isn't very clear. If you could provide an example that would be great too!
CharTermAttributeImpl keeps internally a char array and a length variable that represents the internal term.
The copyBuffer method writes over this array by using the char array provided with the respective offset and length params.
The buffer method returns the internal array that you can directly modify. Additionally, you can get the term representation as a string by calling the attribute's toString method
Have a look at the javadocs for more details: http://lucene.apache.org/core/4_9_0/core/org/apache/lucene/analysis/tokenattributes/CharTermAttribute.html

Runtime method to get names of argument variables?

Inside an Objective-C method, it is possible to get the selector of the method with the keyword _cmd. Does such a thing exist for the names of arguments?
For example, if I have a method declared as such:
- (void)methodWithAnArgument:(id)foo {
...
}
Is there some sort of construct that would allow me to get access to some sort of string-like representation of the variable name? That is, not the value of foo, but something that actually reflects the variable name "foo" in a local variable inside the method.
This information doesn't appear to be stored in NSInvocation or any of its related classes (NSMethodSignature, etc), so I'm not optimistic this can be done using Apple's frameworks or the runtime. I suspect it might be possible with some sort of compile-time macro, but I'm unfamiliar with C macros so I wouldn't know where to begin.
Edit to contain more information about what I'm actually trying to do.
I'm building a tool to help make working with third-party URL schemes easier. There are two sides to how I want my API to look:
As a consumer of a URL scheme, I can call a method like [twitterHandler showUserWithScreenName:#"someTwitterHandle"];
As a creator of an app with a URL scheme, I can define my URLs in a plist dictionary, whose key-value pairs look something like #"showUserWithScreenName": #"twitter://user?screenName={screenName}".
What I'm working on now is finding the best way to glue these together. The current fully-functioning implementation of showUserWithScreenName: looks something like this:
- (void)showUserWithScreenName:(NSString *)screenName {
[self performCommand:NSStringFromSelector(_cmd) withArguments:#{#"screenName": screenName}];
}
Where performCommand:withArguments: is a method that (besides some other logic) looks up the command key in the plist (in this case "showUserWithScreenName:") and evaluates the value as a template using the passed dictionary as the values to bind.
The problem I'm trying to solve: there are dozens of methods like this that look exactly the same, but just swap out the dictionary definition to contain the correct template params. In every case, the desired dictionary key is the name of the parameter. I'm trying to find a way to minimize my boilerplate.
In practice, I assume I'm going to accept that there will be some boilerplate needed, but I can probably make it ever-so-slightly cleaner thanks to NSDictionaryOfVariableBindings (thanks #CodaFi — I wasn't familiar with that macro!). For the sake of argument, I'm curious if it would be possible to completely metaprogram this using something like forwardInvocation:, which as far as I can tell would require some way to access parameter names.
You can use componentsSeparatedByString: with a : after you get the string from NSStringFromSelector(_cmd) and use your #selector's argument names to put the arguments in the correct order.
You can also take a look at this post, which is describing the method naming conventions in Objective C