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 9232883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:25:09+00:00 2026-06-18T06:25:09+00:00

I want to define a method which has a returntype that is dependent on

  • 0

I want to define a method which has a returntype that is dependent on the type of the argument passed to that method and the type parameter of the enclosing trait. It’s quite difficult to describe in words what I mean, so below are some snippets to explain.

I have a few building blocks

// This is the target of a binding
trait BindableValue[T] {
  def set(value: T): Unit

  // this is the method I am strugling with
  def bindTo[S](o: S) = ???
}

// This will dispatch events of type T
trait Observable[T]

// This represents a value of type T that will 
// dispatch events if the value changes
trait ObservableValue[T] extends Observable[T] {
  def value: T
}

// An Observable can be converted to an optional ObservableValue
object ObservableValue {
  implicit def wrap[T](o:Observable[T]):ObservableValue[Option[T]] = 
    new ObservableValue[Option[T]] {
      val value = None
    }
}

A binding has a source and a target and exists for two kinds:

  1. Complete: both source and target type parameters are the same
  2. Incomplete: source and target type parameters are different

…

trait Binding[S, T] {
  def source: ObservableValue[S]
  def target: BindableValue[T]
}

class CompleteBinding[T](
  val source: ObservableValue[T], 
  val target: BindableValue[T]) extends Binding[T, T]

class IncompleteBinding[S, T](
  val source: ObservableValue[S], 
  val target: BindableValue[T]) extends Binding[S, T]

I can construct the following instances.

val bindable1 = new BindableValue[Int] { def set(value:Int) = {} }
val property1 = new ObservableValue[Int] { def value = 0 }
val property2 = new ObservableValue[String] { def value = "" }

val bindable2 = new BindableValue[Option[Int]] { def set(value:Option[Int]) = {} }
val event1 = new Observable[Int] {}
val event2 = new Observable[String] {}

Now I want to use those instances like this:

// 'a' should be of type CompleteBinding
val a = bindable1 bindTo property1

// 'b' should be of type IncompleteBinding
val b = bindable1 bindTo property2

// 'c' should be of type CompleteBinding
val c = bindable2 bindTo event1

// 'd' should be of type IncompleteBinding
val d = bindable2 bindTo event2

I can’t figure out how to define the method bindTo so that it will compile the above 4 lines and have the correct concrete type for all values. I simply miss knowledge about the Scala type system.

Allthough I would love to find a solution I also want to understand how to get to such a solution myself in the future. If you have a solution for the above problem, could you also point me to some sources that I could use to educate myself?

  • 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-18T06:25:10+00:00Added an answer on June 18, 2026 at 6:25 am

    Typeclasses can help you to solve the problem. First let’s define simple trait:

    trait Composeable[A, B, R] {
        def compose(a: A, b: B): R
    }
    

    It just takes a and b and composes them in some other container of type R.

    Now let’s define 2 concrete implementations of it for CompleteBinding and IncompleteBinding in companion object and also make them implicit:

    object Composeable extends LowPriorityComposables {
        implicit def  comCommplete[T] = new Composeable[ObservableValue[T], BindableValue[T], CompleteBinding[T]] {
            def compose(a: ObservableValue[T], b: BindableValue[T]) = new CompleteBinding(a, b)
        }
    }
    
    trait LowPriorityComposables {
        implicit def  comIncommplete[S, T] = new Composeable[ObservableValue[S], BindableValue[T], IncompleteBinding[S, T]] {
            def compose(a: ObservableValue[S], b: BindableValue[T]) = new IncompleteBinding(a, b)
        }
    }
    

    As you can see, implementation is pretty straightforward. It just takes ObservableValue and BindableValue and combines then in Binding. I separated them in different traits because generally they both can be used, if you observe and bind the same value of some type T (the case of CompleteBinding). By extracting comIncommplete in separate trait I told compiler, that it should use it in case if no other suitable implicit was found. In other word you are telling compiler to always try to apply CompleteBinding and if it cannot be done, then IncompleteBinding should be applied.

    The only thing remains is to define bindTo method that uses Composeable type class:

    def bindTo[S, R](o: ObservableValue[S])(implicit ev: Composeable[ObservableValue[S], BindableValue[T], R]): R = 
        ev.compose(o, this)
    

    Here I say, that the first argument is o which is some ObservableValue that contains values of type S, but I also want compile to find an evidence, that there exist some implicit, that proves, that I can compose ObservableValue[S] with BindableValue[T]. When i have such evidence, I just compose observable with this (bindable). As you can see, type parameter R is something like free variable – compiler can figure it out but itself, so you always have concreate type information returned from the bindTo method.

    Now all you test cases should compile just fine:

    val a: CompleteBinding[Int] = bindable1 bindTo property1
    val b: IncompleteBinding[String, Int] = bindable1 bindTo property2
    val c: CompleteBinding[Option[Int]] = bindable2 bindTo event1
    val d: IncompleteBinding[Option[String], Option[Int]] = bindable2 bindTo event2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a class method that has access to a local variable.
I want to define a method that take an integer as input and creates
Assume you define a class, which has a method which does some complicated processing:
Optional method is method which can be applied if class generics has specific type.
I want to make an interface in C# that defines a method that always
I want to define the data.store that store companyList, because I want to reuse
I'm attempting to use the Super Inplace Controls plugin, which has an in_place_select method.
I want to define several methods from an array of method names like so:
I want to define an abstract method like so: public abstract class Saveable {
If I have a method that has the following signature: def max[T <% Ordered[T]](list:List[T]):

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.