<p>I'd like to have both a safe navigation operator and monad unwrapping operator.</p>
<p>Safe navigation is like the feature in TypeScript:</p>
<pre><code>fn() =&gt; print(Some(k)?.property) // prints Some(k.property)
fn() =&gt; print(None?.property) // prints None
</code></pre>
<p>This is very useful for transversing nested structures with members that may or may not exist.</p>
<p>However, I'd also like a monad unwrapping operator like rust has:</p>
<pre><code>fn() =&gt; print(Some(k)?.property) // prints k.property, function returns Some(())
fn() =&gt; print(None(k)?.property) // prints nothing, function returns None
</code></pre>
<p>Note that monad unwrapping works at a function level while safe navigation works at a expression level. Ideally I'd have both, but both contractually use the <code>?</code> symbol creating ambiguity.</p>
<p>Both of these work on many other monads besides <code>Option</code></p>
<p>I already use <code>??</code> for null coalescing.</p>
<p>What other syntax can I use to disambiguate these 2 concepts? Ideally I'd like something that is already used in some other language.</p>

<syntax><monads>
Alternate syntax so I can have both a safe navigation and a monad unwrapping operator?
<h1>Asterisk</h1>
<p>If you're not already using it and it works with your current syntax, you could use <code>*.</code> for monad unwrapping.</p>
<pre><code>Some(k)*.property = k.property
None(k)*.property = return None
</code></pre>

CC BY-SA 4.0
2023-05-17T15:49:21.187Z
2023-05-17T15:49:21.187Z
Ginger
CC BY-SA 4.0
2023-05-17T15:49:47.023Z
Thanks, do you know any language that uses that syntax? I'm specifically looking for a established convention
mousetail 'he-him'
CC BY-SA 4.0
2023-05-17T15:51:20.500Z
@mousetail I don't, but that might just be because no languages have had these two features before. You might get to be a trendsetter!
Ginger
CC BY-SA 4.0
2023-05-17T15:52:18.037Z
(I'm mostly asking for established conventions to avoid being closed as opinion based, I don't actually care)
mousetail 'he-him'
<h1><code>.map</code> method</h1>
<p>This is what Swift does. Using the example from your question:</p>
<pre><code>func printSafe(_ x: T?) {
  print(x?.property)
}

func printMonad(_ x: T?) -&gt; Void? {
  x.map { print($0.property) }
}
</code></pre>
<p>If you provide functional interfaces on collection types, this runs the risk of confusing <code>arr?.map</code> with <code>arr.map</code>.</p>

CC BY-SA 4.0
2023-05-17T16:01:44.733Z
2023-05-17T16:01:44.733Z
Bbrk24
<p>Ruby uses the <code>&amp;.</code> token for safe navigation, which can safely coexist with a <code>?</code> for monads.</p>
<pre><code>foo&amp;.bar # Safe navigation a la Ruby
foo?.bar # Monadic unwrap a la Rust
</code></pre>
<p>If you don't like the asymmetry between <code>?</code> for monad unwrapping and <code>??</code> for null coalescing, I'm quite fond of Perl's <code>//</code> for null coalescing. It looks like <code>||</code> (which is similar) but is clearly a different operator. Only downside is it won't work if you're using C-style comments as well.</p>

CC BY-SA 4.0
2023-05-17T15:52:02.670Z
2023-05-17T15:52:02.670Z
Silvio Mayolo
# Alternate syntax so I can have both a safe navigation and a monad unwrapping operator?
Posted by: username_0 on 2023-05-17
I'd like to have both a safe navigation operator and monad unwrapping operator.
Safe navigation is like the feature in TypeScript:
fn() => print(Some(k)?.property) // prints Some(k.property)
fn() => print(None?.property) // prints None

This is very useful for transversing nested structures with members that may or may not exist.
However, I'd also like a monad unwrapping operator like rust has:
fn() => print(Some(k)?.property) // prints k.property, function returns Some(())
fn() => print(None(k)?.property) // prints nothing, function returns None

Note that monad unwrapping works at a function level while safe navigation works at a expression level. Ideally I'd have both, but both contractually use the ? symbol creating ambiguity.
Both of these work on many other monads besides Option
I already use ?? for null coalescing.
What other syntax can I use to disambiguate these 2 concepts? Ideally I'd like something that is already used in some other language.
**Tags:** syntax monads
---
## 3 answers
### Answer by username_3 on 2023-05-17. Score: 4 | Accepted answer
Ruby uses the &. token for safe navigation, which can safely coexist with a ? for monads.
foo&.bar # Safe navigation a la Ruby
foo?.bar # Monadic unwrap a la Rust

If you don't like the asymmetry between ? for monad unwrapping and ?? for null coalescing, I'm quite fond of Perl's // for null coalescing. It looks like || (which is similar) but is clearly a different operator. Only downside is it won't work if you're using C-style comments as well.
---
### Answer by username_1 on 2023-05-17. Score: 3
Asterisk
If you're not already using it and it works with your current syntax, you could use *. for monad unwrapping.
Some(k)*.property = k.property
None(k)*.property = return None
> **Comments:**
> **username_0** (2023-05-17): Thanks, do you know any language that uses that syntax? I'm specifically looking for a established convention  
> **username_1** (2023-05-17): @mousetail I don't, but that might just be because no languages have had these two features before. You might get to be a trendsetter!  
> **username_0** (2023-05-17): (I'm mostly asking for established conventions to avoid being closed as opinion based, I don't actually care)  
---
### Answer by username_2 on 2023-05-17. Score: 3
.map method
This is what Swift does. Using the example from your question:
func printSafe(_ x: T?) {
  print(x?.property)
}

func printMonad(_ x: T?) -> Void? {
  x.map { print($0.property) }
}

If you provide functional interfaces on collection types, this runs the risk of confusing arr?.map with arr.map.
---
