Ch 18 - Subtyping Flashcards
Think Julia
What is the benefit of unit testing?
Unit Testing
Unit testing allows you to verify the correctness of your code by comparing the results of your code to what you expect. This can be useful to be sure that your code is still correct after modifications, and it is also a way to predefine the correct behavior of your code during development.
Simple unit testing can be performed with the @test macro:
julia > using Test
julia > @test Card( 1, 4) < Card( 2, 4)
Test Passed
julia > @test Card( 1, 3) < Card( 1, 4)
Test Passed
@test returns a “Test Passed” if the expression following it is true, a “Test Failed” if it is false, and an “Error Result” if it could not be evaluated.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6788-6797). Kindle Edition. .
Think Julia
What macro can be used to perform a unit test?
Unit Testing
Unit testing allows you to verify the correctness of your code by comparing the results of your code to what you expect. This can be useful to be sure that your code is still correct after modifications, and it is also a way to predefine the correct behavior of your code during development.
Simple unit testing can be performed with the @test macro:
julia > using Test
julia > @test Card( 1, 4) < Card( 2, 4)
Test Passed
julia > @test Card( 1, 3) < Card( 1, 4)
Test Passed
@test returns a “Test Passed” if the expression following it is true, a “Test Failed” if it is false, and an “Error Result” if it could not be evaluated.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6788-6797). Kindle Edition. .
Think Julia
What does a unit test return?
Unit Testing
Unit testing allows you to verify the correctness of your code by comparing the results of your code to what you expect. This can be useful to be sure that your code is still correct after modifications, and it is also a way to predefine the correct behavior of your code during development.
Simple unit testing can be performed with the @test macro:
julia > using Test
julia > @test Card( 1, 4) < Card( 2, 4)
Test Passed
julia > @test Card( 1, 3) < Card( 1, 4)
Test Passed
@test returns a “Test Passed” if the expression following it is true, a “Test Failed” if it is false, and an “Error Result” if it could not be evaluated.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6788-6797). Kindle Edition. .
Think Julia
What is the term for a method that uses another method to do all the work?
function Base.push!( deck:: Deck, card:: Card)
….push!( deck.cards, card)
….deck
end
A method like this that uses another method without doing much work is sometimes called a veneer. The metaphor comes from woodworking, where a veneer is a thin layer of good-quality wood glued to the surface of a cheaper piece of wood to improve the appearance.
In this case push! is a “thin” method that expresses an array operation in terms appropriate for decks. It improves the appearance, or interface, of the implementation.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6848-6854). Kindle Edition.
Think Julia
What is: subtyping
So, we need a way to group related concrete types. In Julia this is done by defining an abstract type that serves as a parent for both Deck and Hand. This is called subtyping.
Let’s call this abstract type CardSet:
abstract type CardSet end
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6871-6878). Kindle Edition.
Think Julia
How is an abstract type created?
So, we need a way to group related concrete types. In Julia this is done by defining an abstract type that serves as a parent for both Deck and Hand. This is called subtyping.
Let’s call this abstract type CardSet:
abstract type CardSet end
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6871-6878). Kindle Edition.
Think Julia
What is the purpose of subtyping
So, we need a way to group related concrete types. In Julia this is done by defining an abstract type that serves as a parent for both Deck and Hand. This is called subtyping.
Let’s call this abstract type CardSet:
abstract type CardSet end
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 6871-6878). Kindle Edition.