Last active
September 14, 2020 22:17
-
-
Save DSCF-1224/b650ea704331c1c62d7983776280af8c to your computer and use it in GitHub Desktop.
自作複合型に対する演算子オーバーロード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Coordinate | |
| export Rect2d | |
| mutable struct Rect2d{T} | |
| x :: T | |
| y :: T | |
| end | |
| function Base.:+(coordinate_left::Rect2d{T}, coordinate_right::Rect2d{T}) where T <: Core.Number | |
| return Rect2d(coordinate_left.x + coordinate_right.x, coordinate_left.y + coordinate_right.y) | |
| end | |
| function Base.:*(scalar_left::T, coordinate_right::Rect2d{T}) where T <: Core.Number | |
| return Rect2d(scalar_left * coordinate_right.x, scalar_left * coordinate_right.y) | |
| end | |
| function Base.:*(coordinate_left::Rect2d{T}, scalar_right::T) where T <: Core.Number | |
| return Rect2d(coordinate_left.x * scalar_right, coordinate_left.y * scalar_right) | |
| end | |
| function Base.:(==)(coordinate_left::Rect2d, coordinate_right::Rect2d) | |
| return (coordinate_left.x == coordinate_right.x) && (coordinate_left.y == coordinate_right.y) | |
| end | |
| function test_coordinate() | |
| coordinate_a = Rect2d( 1.0, 0.0 ) | |
| coordinate_b = Rect2d( 0.0, 1.0 ) | |
| coordinate_c = coordinate_a + coordinate_b | |
| Base.@assert coordinate_a + coordinate_b == coordinate_c | |
| coordinate_a = Rect2d( 1.0, 2.0 ) | |
| coordinate_b = 2.0 * coordinate_a | |
| coordinate_c = coordinate_a * 2.0 | |
| coordinate_d = Rect2d( 2.0, 4.0 ) | |
| Base.@assert coordinate_b == coordinate_d | |
| Base.@assert coordinate_c == coordinate_d | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment