Here you can see that I can't pass that string parameter
I somehow learned how to pass a method in the same class but, when I want to call it from another class, I can't pass the arguments.
Several issues here:
the introduceMyself method accepts a higher-order function, but in its usage it seems that you don't want the method itself, but its result. I'd suggest replacing the getName parameter with name parameter and convert it to a regular string which will be provided externally by an invocation of said method.
In line 6, you have a syntax error. Since you're using an instance - person, you can replace the :: with a . like so: person.myNameIs("michael"). It's not working because :: passes the method itself, which you don't really want. Again, your code implies that you need to pass the result of the method and not the method itself.
Finally, you can pass higher order functions, but you can't pass them "with an argument". You can do one of 3 things:
Invoke the HOF locally and pass the result of the invocation
Pass the HOF and the argument and invoke them wherever it is you need to.
Pass the HOF and let the target pass whatever argument it chooses.
Related
private fun createData(
index: Int,
address: () -> Address)
and here is the method call
createData(0){Address(...)}
or I can keep simple and instead I can have
private fun createData(
index: Int,
address -> Address)
and I will call just simple way createData(0,Address(...))
I am just wondering in this case is there any benefit to use function type?
The scenario you're showing is a function take takes a "factory" as a parameter. If you are only ever going to immediately call the function exactly one time, then there is no reason not to just take the Address parameter directly.
But suppose creating an Address is a CPU intensive process and you might not need it until later, if at all. Then you could hold onto the function and only use it later if it is truly needed.
Or suppose you want to create a bunch of Addresses using some kind of input parameter. Then you might want to take a parameter like (Int) -> Address and call it multiple times using different parameters.
Or suppose this function is going to need some number of copies of the Address, and Address doesn't have a copy function. Then you could call the function over and over to get copies.
There are many other uses for higher order functions. I'm just giving a few examples of uses of them for a factory, since that's the kind of example you asked about. But a couple of far more general uses:
If the function is supposed to do an action to multiple objects, and you want callers to be able to pass what that action is.
In place of an interface, you can use a function parameter to allow greater flexibility for what is passed to it (a lambda, a defined function, or a SAM interface)
val scoreFragmentArgs1 by navArgs<ScoreFragmentArgs>()
val scoreFragmentArgs2 = ScoreFragmentArgs.fromBundle(arguments!!)
I'm able to access the arguments that was passed from the previous fragment using any of above statements. Can someone explain the difference and when to use each. Thanks in advance
The second call is the simpler one. That one's eagerly evaluated whenever that line runs, so it will require the arguments bundle to be already in place, as well as contain all the keys that you expect to be in it.
The first approach gives you a lazily created Args instance instead, which will only be initialized when you first try reading its value. Therefore it's safe to declare it at the class level. See navArgs in the docs for all the details. The most important parts:
It is strongly recommended that this method only be used when the Activity is started by androidx.navigation.NavController.navigate with the corresponding androidx.navigation.NavDirections object, which ensures that the required arguments are present.
This property can be accessed only after the Activity is attached to the Application, and access prior to that will result in IllegalStateException.
in my projects I'm using lots of AsyncTasks. All they send some data via http to a server. In some of them I pass the data vie execute( data... ). In others I'm using constructor for that:
new SomeTask().execute( 1, 2, 3 );
or
new SomeOtherTask( 1, 2, 3 ).execute();
Basically those calls are equivalent, as the AsyncTask cannot be re-used and is stateless.
What is the preferable way to use those? Am I missing something?
In my opinion, pass parameters to the execute method (if they all share the same type, off course). Using a custom constructor forces you to write it and store parameters in private variables, which is useless, unless these parameters are used in the OnPostExecute method for instance.
Use new SomeTask().execute( 1, 2, 3 );
It is better than passing values to constructor. If you pass your input as argument to constructor, you will need to create member variables and that will be superfluous, since AsyncTask allow us pass variable arguments in execute method.
Until and unless there is need to maintain member variables for the input passed to AsyncTask, constructor should not be used to pass input.
is there Any Way To Make my Method Take many input variable but with out overloading ... could be my question not clear ... I mean Like That :
if I Have This Method
public void setValues (int val1,int val2 ,String val3){
}
what I want is : use this method with many way
setValues (val1,val2)
OR
setValues (val3)
why I want to do that with out overloading : Because if i have as example 10 variable i want to add many method with overloading but i don't like that ...
is there any way helps me to check variable or skip it in the same method ..
Thanks for help .
You can use varargs future to partially solve your problem http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
This can be done if you have parameters of a same type.
But it will require that you will pass variables with the same type as a last param. It is not completely what you want, but it is a small workaround.
No there's no way to do that in Java without method overloading. One alternative would be to group those parameters which are related and make them fields of a class. Then the method would take an instance of that class as a parameter, and which ever parameters were optional would then be null.
Which parameter passing method is used in Android? Pass by reference or Pass by Value? Please provide an Android Specific example for the same.
All parameter passing in Java is by value.
Here is a nice article with all the details.
just like a normal Java Application:
Objects are passed by reference,
primitives are passed by value