Introduce option type (#4150)
* Introduce option type * Improve test naming
This commit is contained in:
		
							parent
							
								
									1974d8f58b
								
							
						
					
					
						commit
						e9955e01d6
					
				
					 2 changed files with 48 additions and 0 deletions
				
			
		
							
								
								
									
										20
									
								
								src/prelude/maybe.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/prelude/maybe.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,20 @@
 | 
			
		|||
export interface Maybe<T> {
 | 
			
		||||
	isJust(): this is Just<T>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export type Just<T> = Maybe<T> & {
 | 
			
		||||
	get(): T
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export function just<T>(value: T): Just<T> {
 | 
			
		||||
	return {
 | 
			
		||||
		isJust: () => true,
 | 
			
		||||
		get: () => value
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function nothing<T>(): Maybe<T> {
 | 
			
		||||
	return {
 | 
			
		||||
		isJust: () => false,
 | 
			
		||||
	};
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue