# Getter functions with constant output

**URL:** https://discourse.julialang.org/t/getter-functions-with-constant-output/117853
**Category:** General Usage
**Tags:** question, book
**Created:** [August 5, 2024, 6:34pm UTC](https://discourse.julialang.org/t/getter-functions-with-constant-output/117853 "2024-08-05T18:34:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jimclay75051](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@jimclay75051](https://discourse.julialang.org/u/jimclay75051)
#### Post date: [August 5, 2024, 6:34pm UTC](https://discourse.julialang.org/t/getter-functions-with-constant-output/117853/1 "2024-08-05T18:34:58Z")

</div>

I am reading “Julia as a Second Language” by Erik Engheim. It seems to be a very good book and I do recogmend it.

However, it is not perfect. For those who have the book, my question refers to chapter eight, page 143, Listing 8.3.

My question has to do with this code:

```julia
abstract type Tank end   

mutable struct SmallTank <: Tank   
    propellant::Float64 
end

mutable struct MediumTank <: Tank  
    propellant::Float64 
end

mutable struct LargeTank <: Tank   
    propellant::Float64 
end

**# Accessor functions (getters)** 
drymass(::SmallTank) = 40.0        
drymass(::MediumTank) = 250.0      
drymass(::LargeTank) = 950.0       

totalmass(::SmallTank) = 410.0     
totalmass(::MediumTank) = 2300.0   
totalmass(::LargeTank) = 10200.0

```

I do not understand what is going on with the last bit, after “# Accessor functions (getters)”:

```julia
**# Accessor functions (getters)** 
drymass(::SmallTank) = 40.0        
drymass(::MediumTank) = 250.0      
drymass(::LargeTank) = 950.0       

totalmass(::SmallTank) = 410.0     
totalmass(::MediumTank) = 2300.0   
totalmass(::LargeTank) = 10200.0

```

From what I can tell these functions do nothing. To use the first one as an example, "drymass(::SmallTank) = 40.0 ". I don’t understand the part inside the parenthesis. I know the “::SmallTank” has to do with typing, but I can’t figure out what it means without a variable. Also, assigning the whole bit to 40.0 seems odd. I suppose that would mean 40.0 is the default amount for the drymass of a SmallTank, but without a parameter it seems to do nothing.

This is just the start of chapter eight. There’s more code to follow, too much to paste here. If anyone has the book and understands whats going on, I would be greatful to have this mystery solved.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [August 5, 2024, 6:38pm UTC](https://discourse.julialang.org/t/getter-functions-with-constant-output/117853/2 "2024-08-05T18:38:42Z")

</div>

This is a one line function definition

```julia
f() = 5

```

is the same thing as

```julia
function f()
    return 5
end

```

---

<div class="post-metadata">

### Author: ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)
#### Post date: [August 5, 2024, 6:43pm UTC](https://discourse.julialang.org/t/getter-functions-with-constant-output/117853/3 "2024-08-05T18:43:18Z")

</div>

`drymass(::SmallTank) = 40.0` is equivalent to

```julia
function drymass(st::SmallTank)
    return 40.0
end

```

Functions like this define the equivalent of class-level constants in other languages, we are saying that every SmallTank has a drymass of 40.0, and any specifics about the tank passed in don’t matter – we only need the type of the variable.

Here’s how you’d use it:

```julia
julia> abstract type Tank end

julia> mutable struct SmallTank <: Tank
           propellant::Float64
       end

julia> x = SmallTank(5.0) # The value here is irrelevant
SmallTank(5.0)

julia> drymass(::SmallTank) = 40.0
drymass (generic function with 1 method)

julia> drymass(x)
40.0

```

---

<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: [August 5, 2024, 7:49pm UTC](https://discourse.julialang.org/t/getter-functions-with-constant-output/117853/4 "2024-08-05T19:49:17Z")

</div>

In terms of syntax, you could write

```julia
drymass(x::SmallTank) = 40.0

```

but you’re not using the variable `x` for anything. Maybe something like

```julia
drymass(_::SmallTank) = 40.0

```

could be used, but it’s just as well, and cleaner too, to just drop it and retain only the type annotation.

---

<div class="post-metadata">

### Author: ![awasserman](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@awasserman](https://discourse.julialang.org/u/awasserman)
#### Post date: [August 5, 2024, 9:05pm UTC](https://discourse.julialang.org/t/getter-functions-with-constant-output/117853/5 "2024-08-05T21:05:52Z")

</div>

See more discussion here:

> [@Defining methods without binding variables](https://discourse.julialang.org/t/defining-methods-without-binding-variables/112485):
>
> I’ve seen many examples of Julia code where methods are defined without any variable name bindings. My usage of terminology my be inaccurate here, but for example: eltype(::Type{\<:AbstractArray{T}}) where {T} = T As far as I can tell, this is equivalent to the following code. eltype(S::Type{\<:AbstractArray{T}}) where {T} = T But since we don’t need to do any computation with the value S, we don’t need to bind the value to this symbol, so we can just use the first way of defining the method,…

I’ve got a docs PR that I should really try to get merged that names and describes this pattern as “anonymous arguments”, as previously suggested.

> <https://github.com/JuliaLang/julia/pull/53948>
>
> Added a description of "anonymous arguments" in the methods section of the manua…l, based on this forum discussion:
> 
> https://discourse.julialang.org/t/defining-methods-without-binding-variables/112485
> 
> This PR chooses a name for the pattern of defining methods without binding values to variables ("anonymous arguments"), and it describes the use of this pattern.
> 
> Someone with a better understanding of the intention of this functionality should review this, as I'm writing it from the perspective of a user rather than a developer.

---

<div class="post-metadata">

### Author: ![jimclay75051](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@jimclay75051](https://discourse.julialang.org/u/jimclay75051)
#### Post date: [August 8, 2024, 1:31am UTC](https://discourse.julialang.org/t/getter-functions-with-constant-output/117853/6 "2024-08-08T01:31:19Z")

</div>

Thanks for the help. I believe I figured out my problem. A rookie mistake.

I was not paying attention to what the getters (seems like they should be called setters) where doing. The drymass is the weight of the tank when empty. The totalmass is the weight of the tank when full. So these amounts are basically constants and should not change. But the tank has three sizes, each with different specifications. By creating functions in this matter the different tank sizes can be selected by the code without using a bunch of if / else statements.

Now that I see what’s happening, it seems pretty simple.
