# money!(player, money(player) + 100)

**URL:** <https://discourse.julialang.org/t/money-player-money-player-100/8967>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 10, 2018, 1:12am UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967 "2018-02-10T01:12:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kungfooman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kungfooman/32/3395_2.png) [@kungfooman](https://discourse.julialang.org/u/kungfooman)\
**Post date:** [February 10, 2018, 1:12am UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/1 "2018-02-10T01:12:18Z")

</div>

Hi all,

does somebody know a good “design pattern” to simplify this kind of code?

`money!(player, money(player) + 100)`

In other languages it would be something like this, via getters/setters:

`player.money += 100`

But I have no “money” attribute, only the methods to read/write the money. I thought about introducing a new type like “PlayerMoney”, which implements `+=` etc.

`money(player) += 100`

Any ideas for a nice/short julian way?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 10, 2018, 1:49am UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/2 "2018-02-10T01:49:07Z")

</div>

How did you define the `money!` function if `player` has no `money` field?

---

<div class="post-metadata">

**Author:** ![kungfooman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kungfooman/32/3395_2.png) [@kungfooman](https://discourse.julialang.org/u/kungfooman)\
**Post date:** [February 10, 2018, 3:19am UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/3 "2018-02-10T03:19:21Z")

</div>

I am using lots of `ccall`s for a game server originally written in C, which has a fixed set of maximal 64 players. So I simply define a player via id:

```julia
type Player <: AbstractEntity
	id::Int32
end

```

So when a new game type needs “player money” I simply “compose” the money to this custom game type, like:

```julia
if !isdefined(:playersMoney)
	playersMoney = zeros(Float32, 64)
end
money( this::Player) = playersMoney[this.id]
money!(this::Player, money) = playersMoney[this.id] = Float32(money)

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 10, 2018, 3:36am UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/4 "2018-02-10T03:36:07Z")

</div>

And why are you against using a `money` field in the `Player` struct? This seems like the most natural way of doing things. If `Player` is only a thin immutable wrapper of an integer, then there is no point defining it, I think. But it seems more natural to throw in all the information of a player in that struct and then pass it around as a whole so you don’t have to worry about many vectors, just a single vector of players.

---

<div class="post-metadata">

**Author:** ![kungfooman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kungfooman/32/3395_2.png) [@kungfooman](https://discourse.julialang.org/u/kungfooman)\
**Post date:** [February 10, 2018, 3:50am UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/5 "2018-02-10T03:50:44Z")

</div>

I don’t like a fixed set of attributes, since depending on the gametype or depending of the entities used inside a map, there might be money or not.

E.g. some maps and gametypes got no shop, so I dont wanna have a “money attribute” in a map without shops.

So if I would put all possible attributes into the player, I would loose this epic composability that Julia offers. I also like that I can quickly define a player by just using `Player(23)` e.g. This is perfect for C callbacks to go from a Int32 to a real player instance.

Yea, at the beginning I somehow worried about those extra `playersMoney` vectors, but I started to like this concept, since I can mix up everything dynamically just as I need it.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 10, 2018, 4:02am UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/6 "2018-02-10T04:02:38Z")

</div>

Hmm, the way I see it you have a couple of options:

1. Define a vector players and put the list of all possible info fields of the players, optional ones can be made `Nullable` or `Union{T,Missing}` using Missings.jl.

```julia
using Missings
mutable struct Player
     id::Int
     money::Union{Float64, Missing}
end

```

1. Define a single struct for all players’ info and put your field vectors in there. Then you can define getters and setters on the PlayersInfo struct directly using the player id.

```julia
struct PlayerID
     id::Int
end
struct PlayersInfo
     money::Vector{Union{Float64,Missing}}
end
getmoney(ps::PlayersInfo, pid::PlayerID) = ps.money[pid.id]
setmoney(ps::PlayersInfo, money, pid::PlayerID) = ps.money[pid.id] = money

```

Also consider using a `Dict` field in the player struct.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [February 10, 2018, 6:33am UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/7 "2018-02-10T06:33:53Z")

</div>

> In other languages it would be something like this, via getters/setters:

On Julia v0.7 and beyond:

> <https://github.com/JuliaLang/julia/commit/4d3d49d2177fe8b1d4b7d5c89882334936b0d665>
>
> New functions \`Base.getproperty\` and \`Base.setproperty!\`
> can be overloaded to c…hange the behavior of \`x.p\` and \`x.p = v\`,
> respectively.
> 
> This forces inference constant propagation through get/setproperty,
> since it is very likely this method will yield better information after specializing on the field name
> (even if \`convert\` is too big to make us want to inline the generic version and trigger the heuristic normally).
> 
> closes #16195 (and thus also closes #16226)
> fix #1974

So I think this will let you get the syntax you want with `ccall`.

---

<div class="post-metadata">

**Author:** ![kungfooman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kungfooman/32/3395_2.png) [@kungfooman](https://discourse.julialang.org/u/kungfooman)\
**Post date:** [February 10, 2018, 5:57pm UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/8 "2018-02-10T17:57:17Z")

</div>

Hi, thanks you two for your inputs, but I am not quite happy with it yet. The custom properties would probably turn out nice, but currently I got dependencies, which only work on 0.6.2

I realised I need some kind of reference into the playersMoney array, so I came up with this:

```julia
importall Base

type ArrayElement{T}
	array::Vector{T}
	index::Int32
end

function (+)(this::ArrayElement{T}, other)::ArrayElement{T} where T
	this.array[this.index] += T(other)
	return this
end

```

So I can add a new `money!` method, but with only one argument:

```julia
money!(this::Player) = ArrayElement{Float32}(playersMoney, this.id)

```

So now I can write something like:

```julia
money!(player1) + 100

```

I think it would be nicer to write this:

```julia
money(player) += 100

```

But I couldn’t figure out any way to overload `+=` yet, that only seems to be supported for `[]` indexing stuff via `getindex`/`setindex!`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 10, 2018, 6:43pm UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/9 "2018-02-10T18:43:02Z")

</div>

I must say that this reads very strangely. You don’t _money_ people, you _pay_ them. I suggest something along these lines:

```julia
pay!(p::Player, sum::Real) = (money[p.id] += sum)

```

where `money` is a dict or something.

---

<div class="post-metadata">

**Author:** ![kungfooman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kungfooman/32/3395_2.png) [@kungfooman](https://discourse.julialang.org/u/kungfooman)\
**Post date:** [February 10, 2018, 6:49pm UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/10 "2018-02-10T18:49:27Z")

</div>

That doesn’t make it any nicer/shorter and is just unnecessary term mixing. I don’t `money` players, I `operate on the money of players`, hence overloading the `+` operator (or `-`, `*` or `/` etc.). Shall I also make up terms for decreasing money, or shall I pay them negative values? That’s leading nowhere.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 10, 2018, 6:56pm UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/11 "2018-02-10T18:56:35Z")

</div>

`money!` is a ridiculous name for that operation. And the operator overloading is odd, and makes the code unintuitive and hard to read.

`pay!` is indeed _much_ nicer. But if you need to arbitrarily set their assets (which you did not make very clear), then something like `setmoney!`, `setassets!` or whatever.

---

<div class="post-metadata">

**Author:** ![NaOH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naoh/32/5632_2.png) [@NaOH](https://discourse.julialang.org/u/NaOH)\
**Post date:** [February 10, 2018, 7:57pm UTC](https://discourse.julialang.org/t/money-player-money-player-100/8967/12 "2018-02-10T19:57:49Z")

</div>

> [@kungfooman](#):
>
> That doesn’t make it any nicer/shorter and is just unnecessary term mixing.

If syntax is the issue then you need Julia’s _syntactic_ macros, for example (just as proof of concept):

```julia
julia> macro set(object::Symbol, update_expr::Expr)
           str_op = string(update_expr.head)
           if !endswith(str_op, "=") && length(str_op) !== 2
               error("syntax: expected update operator, got: `$str_op`.")
           end
           op = Symbol(str_op[1:end-1])
           attr = update_expr.args[1]
           func! = Symbol(attr, :!)
           ammount = update_expr.args[2]
           :($func!($object, $op($attr($object), $ammount))) |> esc
       end
@set (macro with 1 method)

julia> @macroexpand @set player money += 100
:(money!(player, money(player) + 100))

julia> @macroexpand @set spaceship defence -= 42
:(defence!(spaceship, defence(spaceship) - 42))

julia> @macroexpand @set monster speed *= 7
:(speed!(monster, speed(monster) * 7))

julia> @macroexpand @set boss health /= 2
:(health!(boss, health(boss) / 2))

```

- Now you could just say: `@set player money += 100`, etc.
- `@macroexpand` just returns the resulting expression, useful for debugging macros.
- Notice that using `esc` in a whole expression is bad practice, but this is just a simple example, so it doesn’t do much error checking either about the shape of `update_expr` or `str_op`. 😛

> [@DNF](#):
>
> pay! is indeed much nicer.

Agree, verbs are better for this IMHO.
