Getter functions with constant output

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:

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)”:

**# 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.

This is a one line function definition

f() = 5

is the same thing as

function f()
    return 5
end
2 Likes

drymass(::SmallTank) = 40.0 is equivalent to

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> 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
2 Likes

In terms of syntax, you could write

drymass(x::SmallTank) = 40.0

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

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.

4 Likes

See more discussion here:

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.

2 Likes

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.