Replies: 1 comment
-
Yeah, it would be nice, but only as long as the behavior of This solution looks good, though? return switch
{
{ HasNoValue: true } => "a",
{ Value: "X" } => "b"
} This is the same as: if (maybe.HasNoValue)
return "a";
if (maybe.Value == "X")
return "b"; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
@vkhorikov today at work, one of my colleague used a switch expression on a maybe.
and he used it via the Property Pattern matching and not the positional pattern + deconstruct.
it looked something like this:
![image](https://private-user-images.githubusercontent.com/14810960/358072859-24a05d67-d731-44df-b675-bdea32cb6316.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NzM2MTcsIm5iZiI6MTczODg3MzMxNywicGF0aCI6Ii8xNDgxMDk2MC8zNTgwNzI4NTktMjRhMDVkNjctZDczMS00NGRmLWI2NzUtYmRlYTMyY2I2MzE2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA2VDIwMjE1N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNiODhlNzEwMDU5NDdiMzRjZGI4N2FjMGFjYTg3ZmU5ODUwNTI1N2NiZWZhODllNDhiNmM5NjAzMTI4M2YzNDgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.5tnOKRjbg2ba2LwDKxh-4yfwNyJHLDZfUurnh5JwFUE)
and because of the current implementation of the Value property is was breaking as expected
![image](https://private-user-images.githubusercontent.com/14810960/358064772-07a473f8-4810-47e9-885b-78ae3de58ecb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NzM2MTcsIm5iZiI6MTczODg3MzMxNywicGF0aCI6Ii8xNDgxMDk2MC8zNTgwNjQ3NzItMDdhNDczZjgtNDgxMC00N2U5LTg4NWItNzhhZTNkZTU4ZWNiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA2VDIwMjE1N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNlMDZjMjFkNjFkM2RjMDM3YWVjZTY3YTAzMGU1MDYzNDIwOTdhZDM4ZjE4Mzc5M2ZlY2JhM2MzNTk0YjlhMzkmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.oxzE8S9NUnCoNBtGR52boTEFQNDQHSAMJxnm9YToxG4)
so every time you wish you use switch expression with property matching it would fail if you try to switch on the Value property value.
so the only way to use a switch expression with the Maybe type is via the positional pattern matching that uses the deconstruct like this
![image](https://private-user-images.githubusercontent.com/14810960/358071113-012cb9c8-4533-489c-a782-2be9c353e8fb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NzM2MTcsIm5iZiI6MTczODg3MzMxNywicGF0aCI6Ii8xNDgxMDk2MC8zNTgwNzExMTMtMDEyY2I5YzgtNDUzMy00ODljLWE3ODItMmJlOWMzNTNlOGZiLnBuZz9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kSGVhZGVycz1ob3N0In0.pTacYa9ihZ2QNDqN4Ur2-KEbu1zRqK1ufUSHg4pLqtQ)
or
with the Property Matching with some ordering like this, to validate the HasValue first
or be robust upfront with a If, or use some safety execution extension like the Maybe.Execute
so i was thinking, okay i'll push some changes, to simply return the _value instead of GetValueOrThrow but that's a massive breaking change.
It would be nice to have support that feature out of the box, but it implies a huge behaviour change.
Beta Was this translation helpful? Give feedback.
All reactions