docs: update smart casting documentation (#7884)
							parent
							
								
									7c290a7fe8
								
							
						
					
					
						commit
						a8741fdced
					
				
							
								
								
									
										14
									
								
								doc/docs.md
								
								
								
								
							
							
						
						
									
										14
									
								
								doc/docs.md
								
								
								
								
							|  | @ -1888,18 +1888,20 @@ if w is Mars { | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
| `w` has type `Mars` inside the body of the `if` statement. This is | `w` has type `Mars` inside the body of the `if` statement. This is | ||||||
| known as *flow-sensitive typing*. You can also specify a variable name: | known as *flow-sensitive typing*. | ||||||
|  | If `w` is a mutable identifier, it would be unsafe if the compiler smart casts it without a warning. | ||||||
|  | That's why you have to declare a `mut` before the `is` expression: | ||||||
| 
 | 
 | ||||||
| ```v ignore | ```v ignore | ||||||
| if w is Mars as mars { | if mut w is Mars { | ||||||
|     assert typeof(w).name == 'World' |     assert typeof(w).name == 'Mars' | ||||||
|     if mars.dust_storm() { |     if w.dust_storm() { | ||||||
|         println('bad weather!') |         println('bad weather!') | ||||||
|     } |     } | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
| `w` keeps its original type. This form is necessary if `w` is a more | Otherwise `w` would keep its original type. | ||||||
| complex expression than just a variable name. | > This works for both, simple variables and complex expressions like `user.name` | ||||||
| 
 | 
 | ||||||
| #### Matching sum types | #### Matching sum types | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue