The fact that x += y modifies lists in place might be surprising if you're expecting it to be exactly equivalent to x = x + y.
shape_warrior_t
There was a recent langdev Stack Exchange question about this very topic. It's a bit trickier to design than it might seem at first.
Suppose we require a keyword -- say var -- before all binding patterns. This results in having to write things like
for (&(var x1, var y1, var z1), &(var x2, var y2, var z2)) in points.iter().tuple_windows() {},
which is quite a bit more verbose than the current
for (&(x1, y1, z1), &(x2, y2, z2)) in points.iter().tuple_windows() {}.
Not to mention you'll have to write let var x = 0; just to declare a variable, unless you redesign the language to allow you to just write var x = 0 (and if you do that, you'll also have to somehow support a coherent way to express if let Some(x) = arr.pop() {} and let Some(x) = arr.pop() else {todo!()}).
Suppose we require a keyword -- say const -- before all value-matching patterns that look like variables. Then, what's currently
match (left.next(), right.next()) {
(Some(l), Some(r)) => {}
(Some(l), None) => {}
(None, Some(r)) => {}
(None, None) => {}
}
turns into either the inconsistently ugly
match (left.next(), right.next()) {
(Some(l), Some(r)) => {}
(Some(l), const None) => {}
(const None, Some(r)) => {}
(const None, const None) => {}
}
or the even more verbose
match (left.next(), right.next()) {
(const Some(l), const Some(r)) => {}
(const Some(l), const None) => {}
(const None, const Some(r)) => {}
(const None, const None) => {}
}
and you always run the risk of forgetting a const and accidentally binding a new match-all variable named None -- the main footgun that syntactically distinguishing binding and value-matching patterns was meant to avoid in the first place.
Suppose we require a sigil such as $ before one type of pattern. Probably the best solution in my opinion, but that's one symbol that can no longer be used for other things in a pattern context. Also, if you're already using sigils before variable names for other purposes (I've been sketching out a language where a pointer variable $x can be auto-dereferenced by writing x), doubling up is really unpleasant.
...So I can understand why Rust chose to give the same, most concise possible syntax for both binding and value-matching patterns. At least compiler warnings (unused, non-snake-case variables) are there to provide some protection from accidentally turning one into the other.
I was thinking of the three legal states as:
- not logged in (
nullor{isAdmin: false, isLoggedIn: false}) - logged in as non-admin (
falseor{isAdmin: false, isLoggedIn: true}) - logged in as admin (
trueor{isAdmin: true, isLoggedIn: true})
which leaves {isAdmin: true, isLoggedIn: false} as an invalid, nonsensical state. (How would you know the user's an admin if they're not logged in?) Of course, in a different context, all four states could potentially be distinctly meaningful.
My preferred way of modelling this would probably be something like
role: "admin" | "regular" | "logged-out"
or
type Role = "admin" | "regular";
role: Role | null
depending on whether being logged out is a state on the same level as being a logged-in (non-)admin. In a language like Rust,
enum Role {Admin, Regular}
instead of just using strings.
I wouldn't consider performance here unless it clearly mattered, certainly not enough to use
role: number,
which is just about the least type-safe solution possible. Perhaps
role: typeof ADMIN | typeof REGULAR | typeof LOGGED_OUT
with appropriately defined constants might be okay, though.
Disclaimer: neither a professional programmer nor someone who regularly writes TypeScript as of now.
a === b returns true if a and b have the same type and are considered equal, and false otherwise. If a is null and b is a boolean, it will simply return false.
I would certainly rather see this than {isAdmin: bool; isLoggedIn: bool}. With boolean | null, at least illegal states are unrepresentable... even if the legal states are represented in an... interesting way.
Nice! Some feedback (on your Python, I don't speak Greek):
- In Python, the idiomatic way to name variables and functions is
snake_case-- for example,greekTranslationshould begreek_translation. (EDIT after seeing your most recent reply: good luck with that when it comes to Python :) ) - You're currently recomputing
reverseTranslationevery time the user requests an English-to-Greek translation. Unless you're planning to modifygreekTranslationwhile the program is running, it would likely be more efficient to makereverseTranslationa global variable, computed just once at the start of the program. - The control flow in the main program could be a bit more clear:
- The condition in
while optionSelection == <'practice'/'translation'>makes it look likeoptionSelectioncould change to something else inside the loop, yet you never do so. You could just writewhile Trueinstead. - Instead of putting the "Please select either practice, translate, or exit" check up front, it would likely be more maintainable to add it as an
elsebranch to the mainif-elif-elifchain. That way, if you added in a new option, you would only have one place in the code to modify instead of two. (Also, instead ofx != a and x != b and x != c, you could writex not in [a, b, c]). - Speaking of the
if-elif-elifchain, you could replace it with amatchstatement, which would remove the repetitions ofoptionSelection ==.
- The condition in
Here's how I would write the control flow for the last section:
optionSelection = input().strip().lower()
match optionSelection:
case 'practice':
while True:
practiceGreek()
print('')
print('Would you like another? [yes] [no]')
print('')
selection = input().strip().lower()
print('')
if selection != 'yes':
break
case 'translate':
while True:
translateToGreek()
print('')
print('Would you like to translate another phrase? [yes] [no]')
print('')
selection = input().strip().lower()
print('')
if selection != 'yes':
break
case 'exit':
print('')
print('Thank you for using the Greek Practice Program!')
print('')
sys.exit()
case _:
print('')
print('Please select either practice, translate, or exit')
print('')
(You could probably pull the while True loops into their own dedicated functions, actually.)
Hope that helps. Feel free to ask if any of this doesn't make sense.
Even regular Rust code is more "exciting" than Python in this regard, since you have a choice between self, &self, and &mut self. And occasionally mut self, &'a self, and even self: Box<Self>. All of which offer different semantics depending on what exactly you're trying to do.
You can get the exclusive behaviour with random.randrange. (Relevant Stack Overflow question with a somewhat interesting answer)
Probably a good idea, but not one that fits my current vision of the game, unfortunately. Especially since my main idea for adding complexity is increasing the number of sides (square->pentagon->hexagon), so each level already has a different-looking playfield.
...Right, I kind of forgot that the directory structure for my repo doesn't need to look anything like the directory structure of the releases. Thanks for prompting me to think about that.
Not very familiar with the libraries, and BB_C has already given a lot of more detailed feedback, but here are some small things:
get_files_in_diras a single chain of method calls, you can still replace with In general, if you can useif letorlet elseto pattern match, prefer that over unwrapping -- Clippy has a lint relating to this, actually.return Noneinget_files_in_dir.