is there a way to declare a variable with the value of a function which takes this declaring variable as a parameter?

choochosan044

i am wondering is there some syntax in Kotlin for declaring a variable which will be equal to a function which takes several parameters 1 of which is said variable?

class Player(var deck: MutableList<String> = Deck().deck) {

var playerHand = Deck().deal(deck, playerHand, 6)

}

class Deck {

var deck = mutableListOf<String>()

fun deal( from: MutableList<String>, to: MutableList<String>, num: Int ){
        var temp = from.slice(0 .. num).toMutableList()
        to.addAll(temp)
        from.removeAll(temp)
    } 
}

So i basically want to transfer N amount of cards from a deck inside a Deck class to a variable playerHand in Player class. I know there are numerous other ways of doing it, i just wanted to know is there a way to create a var and assign a value to it using a function that takes that var as a parameter..

Tenfour04

I stared at your code for a while and finally realized what you're asking, I think: You want playerHand to be a MutableList whose initial values come from calling Deck().deal(). Like @broot says in the comment, the standard way to do this is to use also. I'm also assuming it's some kind of typo that you are instantiating a new Deck instance to deal from, when you already have a deck property in your class. However, that property should probably be a Deck instance directly so you still have access to the Deck functionality that goes with it.

You might consider making your Deck class implement MutableList using a delegate to simplify how it is referenced and used. Now you can directly use existing MutableList functions like shuffle() directly on your Deck instance. And I would use LinkedList specifically as the delegate because it is more efficient than the default returned by mutableListOf at removing values from the top.

I would also make the deal function simply a dealTo function because it should be assumed a Deck is only going to deal from itself.

Putting that all together:

class Player(var deck: Deck = Deck()) {

    val playerHand = mutableListOf<String>().also {
        deck.dealTo(it, 6)
    }

}

class Deck: MutableList<String> by LinkedList<String>() {

    fun dealTo(to: MutableList<String>, num: Int) {
        repeat(num) {
            to += removeFirst()
        }
    }
    
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to assign a variable which function takes as a parameter

How do i declare a function which takes a variable of a yet unidentified type as parameter

Which way is better for declaring class variable with constants initial value?

In JavaScript: Is a function parameter variable (at the moment of function declaration) equivalent to declaring a variable?

Which is the best way to declare logger variable in java

Declaring macro variable with evaluation of function as value

Is there a way to declare a variable by adding a variable name with a variable value?

Is there any way to declare a function as a variable in perl?

Is there a way that I can declare the variable outside function?

Declaring variable outside a function

Declaring variable within a function

Increase value of variable by parameter of function

Is there a way to declare method that takes any lambda as parameter?

PHP Declare and Assign Variable (Same Name as Function Name) Before Declaring A Function

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

How to create HTML element which takes value from variable

Conditional color of points depending on which variable takes a specific value

Why can't I declare a variable that takes the value of a dereferenced variable in Assembly?

SQL Function with Declare Variable

Declaring new variable in recursive function

Using a function directly or by declaring it to a variable

Is declaring a function variable required (in Javascript)?

Calling a function like declaring a variable

Are we declaring a function as a method on a variable?

declaring a variable without returning a value .

In which situation to declare instance variable?

Declare variable as function value at a given time - not the function itself

A better way to declare function name based on variable name?

Is there a way to declare a variable sized vector globally from a function?