#Naming convenction for unwrapped value

1 messages · Page 1 of 1 (latest)

honest burrow
#

Hello,

I sometimes have situations, where I am naming something, that is nullable, and then I need to give it new name, in order to unwrap it for specific code block, what always leads me to the right naming problem. Is there any covenction for that?
Example:

token_type: ?TokenType = null;
for (...) |...| {
  token_type = get_token(...)
  if (token_type) |naminng_issue| {
    ...
  }
}

Thank for help!

modest flicker
honest burrow
#

Ok, thank you, if this is kind of convenction, i will use it, but i must say that it sounds wierd for me ;/
I am not sure why zig force that renaming, for block of code, under if like that

modest flicker
#

you dont need to rename it, you can do if (maybe_str) |_| print(maybe_str.?)

#

Zig just doesnt want shadowing

honest burrow
#

ye, but with that, i need to do that at all places ;/

#

anyway, thank you!

#

doeas that renaming, works as reassigning? i mean, do i create copy, and operate on copy?

modest flicker
honest burrow
#

Also i am confused by one thing at zig, related to that

#
maybe_token_type: TokenType = null;
// Here I am able to evaluate if token_tupe is null, simply by providing (variable_name), like in python. 
if (maybe_token_type) |token_type| { 
  ...
}
// Here I am getting error: expected type 'bool', found '?input_parser.TokenType' 
if (maybe_token_type) { 
  ...
}
modest flicker
#

Afaik this is just to make code more readable, they dont want an implicit |_| for optional if statements