This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ICE reproduction for issue rust-lang/rust#79768.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#![feature(generic_associated_types)] | ||
|
||
trait Monad /* : Applicative (for pure/return, doesn't matter for this example) */ { | ||
// Self is like the "f a" in haskell | ||
|
||
/// extract the "a" from "f a" | ||
type Unplug; | ||
|
||
/// exchange the "a" in "f a" in the type of Self with B | ||
type Plug<B>: Monad; | ||
|
||
fn bind<B, F>(self, f: F) -> Self::Plug<B> | ||
where | ||
F: Fn(Self::Unplug) -> Self::Plug<B>; | ||
} | ||
|
||
impl<A> Monad for Option<A> { | ||
type Unplug = A; | ||
type Plug<B> = Option<B>; | ||
fn bind<B, F>(self, f: F) -> Option<B> | ||
where | ||
F: Fn(A) -> Option<B>, | ||
{ | ||
self.and_then(f) | ||
} | ||
} | ||
|
||
// This function causes the compiler error, specifically, when i added the Plug = P constraint. | ||
fn stringify<T, M1, P>(m: M1) -> M1::Plug<P> | ||
where | ||
T: core::fmt::Display, | ||
M1: Monad<Unplug = T, Plug = P>, | ||
{ | ||
m.bind(|x| format!("{}", x)) | ||
} | ||
|
||
fn main() { | ||
let x = Some(1).bind(|x| Some(x * 2)); | ||
println!("{:?}", x); | ||
} |