Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:25:43+00:00 2026-06-09T12:25:43+00:00

In short, I want to declare a trait like this: trait Test { def

  • 0

In short, I want to declare a trait like this:

trait Test {
    def test(amount: Int): A[Int] // where A must be a Monad
}

so that I can use it without knowing what monad that A is, like:

class Usecase {
    def someFun(t: Test) = for { i <- t.test(3) } yield i+1
}

more details…

essentially, I want to do something like this:

class MonadResultA extends SomeUnknownType {
    // the base function
    def test(s: String): Option[Int] = Some(3)
}

class MonadResultB(a: MonadResultA) extends SomeUnknownType {
    // added a layer of Writer on top of base function
    def test(s: String): WriterT[Option, String, Int] = WriterT.put(a.test(s))("the log")
}

class Process {
    def work(x: SomeUnknownType) {
        for {
            i <- x.test("key")
        } yield i+1
    }
}

I wanted to be able to pass any instances of MonadResultA or MonadResultB without making any changes to the function work.
The missing piece is that SomeUnknowType, which I guess should have a test like below to make the work function compiles.

trait SomeUnknowType {
    def test(s: String): T[Int] // where T must be some Monad
}

As I’ve said, I’m still learning this monad thing… if you find my code is not the right way to do it, you’re more than welcomed to point it out~

thanks a lot~~

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:25:45+00:00Added an answer on June 9, 2026 at 12:25 pm

    Assuming you have a type class called Monad you can just write

    def test[A:Monad](amount: Int): A[Int]
    

    The compiler will require that there is an implicit of type Monad[A] in scope when test is called.

    EDIT:
    I’m still not sure what you’re looking for, but you could package up a monad value with its corresponding type class in a trait like this:

    //trait that holds value and monad
    trait ValueWithMonad[E] {
      type A[+E]
      type M <: Monad[A]
      val v:A[E]
      val m:M
    }
    
    object M {
    
      //example implementation of test method
      def test(amount:Int):ValueWithMonad[Int] = new ValueWithMonad[Int] {
        type A[+E] = Option[E]
        type M = Monad[Option]
        override val v = Option(amount)
        override val m = OptionMonad
      }
    
      //test can now be used like this
      def t {
        val vwm = test(1)
        vwm.m.bind(vwm.v, (x:Int) => {
          println(x)
          vwm.m.ret(x)
        })
      }
    }
    
    trait Monad[A[_]] {
      def bind[E,E2](m:A[E], f:E=>A[E2]):A[E2]
      def ret[E](e:E):A[E]
    }
    
    object OptionMonad extends Monad[Option] {
      override def bind[E,E2](m:Option[E], f:E=>Option[E2]) = m.flatMap(f)
      override def ret[E](e:E) = Some(e)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to consult SQL Server OR short-circuit Code: DECLARE @tempTable table ( id
In short: I want to have two fullscreen views, where I can switch between
I want to develop a short url system, like bitly, and I have a
I want to test whether a certain string is contained in a short list
In short, I want to use an object literal to allow me to pass
In short: For this code: Encoding.ASCII.GetBytes(‚) I want the output to be 130, but
I declared an enum: public enum HeightTypes{ Tall, Short} Now I want to use
I want to declare a new enum with non-default underlying type. This works: public
I want to declare some php functions and i would like to call those
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.