-
-
Notifications
You must be signed in to change notification settings - Fork 73
Type unions concept #921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type unions concept #921
Conversation
Good to hear you're well and back in action! On my end, I currently have decent availability, but poor internet. Starting May 24th (ish), I should have decent internet, but likely more limited availability. So, potentially not much change in the near future, but I'll continue to try to keep up. That said, I'll see if I can come up with a first draft of an exercise for the
On a first look through this, it looked good and I didn't catch anything to note. Where a suggestion popped up, I found it already incorporated later on :) |
Thanks for the update. Meanwhile, I have plenty to keep me busy, even if you aren't able to review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a small subject so I don't see anything to add at present, but I'll keep my eyes open. Everything else looks good!
I've had a look at the Gleam exercise you mentioned. I could try adapting that if you haven't gotten into it as I should have pretty good availability for the next week. I'm curious what prereqs you were thinking. The |
It's a couple of weeks since I looked at struct Player
name::Union{String, missing}
level::Int64
health::Int64
mana::Union{Int64, nothing}
end
introduce(player::Player) = ismissing(player.name) ? "Mighty Magician" : player.name
function revive(player::Player)
player.health > 0 || return nothing
player.level >= 10 ? Player(player.name, player.level, player.health, 100) :
Player(player.name, player.level, 100, player.mana)
end
function cast_spell(player::Player, cost::Int64)
player.mana < cost && return (player, 0)
isnothing(player.mana) &&
return (Player(player.name, player.level, player.health - cost, player.mana), 0)
(Player(player.name, player.level, player.health - damage, player.mana - cost), cost * 2)
end I don't know if it's any use, but feel free to use it or scrap it. My views on prereqs changed since I submitted the draft PR. I see this as needing |
Great! I'll give it a look |
I need to do more on the Julia syllabus, now you're back. I felt I needed a break for a few days, doing other stuff. Over the weekend, I wrote a few things for Kotlin, which ended up getting exactly zero response. Yesterday (between the psychodramas!) I started working through the Gleam syllabus, to see what it looks like from a student perspective. It's a few years since I did some exercises on this track, and the syllabus didn't exist then. Nice language, but I don't really have a use case for the BEAM. |
I'll be happy to see what we can get done in the next week if your up for it! After that, my availability may take a nose dive again for a couple of weeks, but I'll try as ever to stay in touch with any developments. |
I just found one more bit, in using Test
include("role-playing-game.jl")
@testset verbose = true "tests" begin
@testset "1. introduce" begin
@testset "With their own name" begin
player = Player(name = "Gandalf", level = 1, health = 42, mana = nothing)
end
end
end It was at that point that I realized I needed to rewrite the exemplar code to use It was one of those days... |
Along with the A first thought: When I first saw the Gleam version, I thought it could be nice to introduce two methods with different function signatures (which would give a look ahead to multiple dispatch without explaining anything). For example @kwdef mutable struct Player
name::Union{String, missing}
level::Int
health::Int
mana::Union{Int, nothing}
end
introduce(player::Player) = ismissing(player.name) ? "Mighty Magician" : player.name
increment(mana::Union{Int, nothing}, level::Int) = level ≥ 10 ? 100 : mana
increment(level::Int, multiplier::Int) = min(level + multiplier, 42)
function revive(player::Player)
player.health > 0 || return nothing
player.health = 100
player.mana = increment(player.mana, player.level)
end
function castspell(player::Player, cost::Int)
if isnothing(player.mana)
player.health -= cost
player, 0
elseif player.mana < cost
player, 0
else
player.mana -= cost
player, 2cost
end
end
function levelup(player::Player, increase::Int)
multiplier = player.mana ≥ 100 ? 2 : 1
player.level = increment(player.level, multiplier)
end A potential stub could look like: @kwdef mutable struct Player
end
function introduce(player::Player)
end
function increment(mana::Union{Int, nothing}, level::Int)
end
function increment(level::Int, multiplier::Int)
end
function revive(player::Player)
end
function castspell(player::Player, cost::Int)
end
function levelup(player::Player, increase::Int)
end With this, it would be also possible to have the student name the type union IntOrNothing = # define type union
@kwdef mutable struct Player
end
function introduce(player::Player)
end
function increment(mana::IntOrNothing, level::Int)
end
function increment(level::Int, multiplier::Int)
end
function revive(player::Player)
end
function castspell(player::Player, cost::Int)
end
function levelup(player::Player, increase::Int)
end Let me know what you think about anything I've floated here. In particular, I'm not sure if this is an appropriate way to hint at multiple dispatch or if it's too advanced or whatnot. |
Yes! This is the sort of fresh thinking the exercise needed - a straight port from Gleam wasn't satisfying me. Go for it, and good luck! |
I should have said this is entirely flexible. It could go below |
Agreed! At present, personally, I would like to see it beside Also, if we wanted to push it a bit more as a hint at |
I've gone ahead and merged this, accepting that it may need a few changes later to align with the exercise. |
I'm hoping that
gleam/role-playing-game
can be adapted to pair with this.Though this concept is relatively short and simple, it's been in the works for a couple of weeks. I had some medical stuff going on (yet again).
Now I'm (hopefully) back in action, I'll try and put together the long-awaited Multiple Dispatch concept, to round out the group on types. Once I have all the concept docs, I'll go back and work on the exercises.