passing null as aparameter to listOf - kotlin

can you please tell me what does the second parameter passed to the method listOf means? why do we need to pass null? why is the purpose of it?
code:
val listWithNulls: List<String?> = listOf("Kotlin", null)

listOf accepts a vararg of elements. Your second null is just one of many elements you passed to the list. Your list then consists of "Kotlin" and null.
So to answer your other questions:
why do we need to pass null? you don't need to...
what is the purpose of it? the person who wrote that code wanted to add a null element to the list... if it isn't clear from the context, why it is there, you should better ask the one who wrote that code. If it just consists of the line you showed, it's probably to demonstrate how lists with a nullable type can be constructed/used.

Related

How can we remove similar items in a list in Kotlin? [duplicate]

This question already has answers here:
How can I remove duplicate objects with distinctBy from a list in Kotlin?
(3 answers)
Closed 1 year ago.
Do we have any specific Function to do that or we can use the normal for-loop logic
Eg:-
// we have a list, say carList, which is initially empty
val carList = mutableListOf<Cars>()
carList.add(randomCar()) // adding any random car
// so I want to know that , if there is any duplicate entry, how can I remove it from carList ?
You can do this either by converting the list into a set or by using the distinct function.
Here's a reference: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/distinct.html
If you want to be sure there are no duplication, then I suggest to use set instead of list. Using list for such usage would be more complex by requiring extra looping, checking and removal. Be aware that any comparison between two elements is based on implementations of methods equals and hashCode (Kotlin data class provide such functionality out of box by implementing these functions for you).
There's list.contains(...). You can check for the existence of the variable and if it exists, run the Random Generator again. Or if you wish to just remove the previous item then add this same one, or whatever you know. There's removeFilter too, if you wish to achieve something more complex, but this is not what I think will be required for your use case.
Thanks

Kotlin syntax issue

Sorry for the terrible title, but I can't seem to find an allowable way to ask this question, because I don't know how to refer to the code constructs I am looking at.
Looking at this file: https://github.com/Hexworks/caves-of-zircon-tutorial/blob/master/src/main/kotlin/org/hexworks/cavesofzircon/systems/InputReceiver.kt
I don't understand what is going on here:
override fun update(entity: GameEntity<out EntityType>, context: GameContext): Boolean {
val (_, _, uiEvent, player) = context
I can understand some things.
We are overriding the update function, which is defined in the Behavior class, which is a superclass of this class.
The update function accepts two parameters. A GameEntity named entity, and a GameContext called context.
The function returns a Boolean result.
However, I do not understand the next line at all. Just open and close parentheses, two underscores as the first two parameters, and then an assignment to the context argument. What is it we are assigning the value of context to?
Based on IDE behavior, apparently the open-close parentheses are related to the constructor for GameContext. But I would not know that otherwise. I also don't understand what the meaning is of the underscores in the argument list.
And finally, I have read about the declaration-site variance keyword "out", but I don't really understand what it means here. We have GameEntity<out EntityType>. So as I understand it, that means this method produces EntityType, but does not consume it. How is that demonstrated in this code?
val (_, _, uiEvent, player) = context
You are extracting the 3rd and 4th value from the context and ignoring the first two.
Compare https://kotlinlang.org/docs/reference/multi-declarations.html .
About out: i don't see it being used in the code snippet you're showing. You might want to show the full method.
Also, maybe it is there only for the purpose of overriding the method, to match the signature of the function.
To cover the little bit that Incubbus's otherwise-great answer missed:
In the declaration
override fun update(entity: GameEntity<out EntityType>, // …
the out means that you could call the function and pass a GameEntity<SubclassOfEntityType> (or even a SubclassOfGameEntity<SubclassOfEntityType>).
With no out, you'd have to pass a GameEntity<EntityType> (or a SubclassOfGameEntity<EntityType>).
I guess that's inherited from the superclass method that you're overriding.  After all, if the superclass method could be called with a GameEntity<SubclassOfEntityType>, then your override will need to handle that too.  (The Liskov substitution principle in action!)

How to programmatically get the inner type of a container in Julia? [duplicate]

Suppose I have an object which has a type like NullableArray{Int64} -- how do I know that the elements have type Int64 (well, Nullable{Int64}) without actually accessing any element (ie, it can be done when the object is empty). Is there a general way to figure out what SubType is from an object of type Type{SubType}?
Edit: Whoops, to expand on the accepted answer below I realized I was doing eltype(x) but needed to do eltype(eltype(x)) for the example of Nullables.
you can use eltype
assert(eltype(collect(1:3)) == Int)

Difference between using GetterUtils and ParamUtils

For instance, when to use
GetterUtil.getBoolean()
and when
ParamUtil.getBoolean()?
Are both same, or is it expected to be used differently according to a parameter, a variable, etc? Can you give some examples for both?
Both are util methods to avoid Null-Pointer Exceptions.
GetterUtil internally returns the default type and does the casting too. So in case where someone has passed a null value, it will return default value of the type.
Example:
Assume you have a String value "true", and you are expecting it will always be of type boolean. So you use GetterUtil.getBoolean("true") which will internally do the casting to boolen and return the value as boolean-true. Incase someone passes rubbish characters like "tr", it will be converted to boolean-false.
As mentioned ParamUtil does the same treatment with request parameters. ParamUtil internally uses the GetterUtil to have the above behaviour. It first retrieves the parameter (which always would be a string) and then passes it to GetterUtil.getType() method and in turn returns the proper type.
GetterUtil and ParmUtil both are different classes.
GetterUtil is to get the default values for basic Java data types.
ParamUtil is to retrive the values(of primitive data types) from the HttpReqeust.
Check the source code here for these two classes here
For GetterUtil
http://docs.liferay.com/portal/6.0/javadocs/src-html/com/liferay/portal/kernel/util/GetterUtil.html
For ParamUtil
http://docs.liferay.com/portal/5.1/javadocs/portal-kernel/com/liferay/portal/kernel/util/ParamUtil.java.html

What is the definition of ":=" in vb

I came across some sample code in VB.Net which I have some experience with and kinda having a duh moment figuring out the meaning of :=.
RefreshNavigationImages(bForward:=True, startIndex:=-1)
The sig for this method is RefreshNavigationImages(boolean, int). Is this a default value if null? Like "bIsSomething ?? false"?
Tried to bing/google but they just don't like searching for operators especially if it's only 2 chars.
They are named parameters. They let you specify values for arguments in function calls by name rather than order.
The := indicates the use of named parameters. Rather than relying on the order of the parameters in the method declaration, named parameters allow you to specify the correlation of parameters to values by specifying the name of the parameter.