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

  • SEARCH
  • Home
  • 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 8211353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:24:02+00:00 2026-06-07T10:24:02+00:00

Is it possible to define generic operators in Scala? Scala lets me map arbitrary

  • 0

Is it possible to define generic operators in Scala?

Scala lets me map arbitrary operators on functions, which is incredibly useful. It seems restrictive however, in a case where I might want the operators to change given the state of the application.


To give an example: I have a table with users and a table with their respective relationships. Each relationship has a type, such as: “friends-with”, “works-with”, etc.
Based on my domain model, I would like the DSL to allow for: is(john friends-with mary). In this case, both john and mary would be of Object User, which would have a generic operator def <relationship> (a:User): Boolean = {...}.


What I wanted to achieve was exactly what Dynamic would allow me to do (see answer). The description fits perfectly:

A marker trait that enables dynamic invocations. Instances x of this trait allow calls x.meth(args) for arbitrary method names meth and argument lists args. If a call is not natively supported by x, it is rewritten to x.applyDynamic(“meth”, args).

More information here: http://www.scala-lang.org/api/current/scala/Dynamic.html

  • 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-07T10:24:03+00:00Added an answer on June 7, 2026 at 10:24 am

    Look at trait Dynamic, which will be available on Scala 2.10 (it’s experimental on Scala 2.9).

    For example:

    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    case class User(name: String) extends Dynamic {
      def applyDynamic(relationship: String)(to: User) =
        Relation(relationship, this, to)
    }
    case class Relation(kind: String, from: User, to: User)
    
    // Exiting paste mode, now interpreting.
    
    defined class User
    defined class Relation
    
    scala> User("john") friendsWith User("mary")
    res0: Relation = Relation(friendsWith,User(john),User(mary))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's possible to define an alias in C# like this using kvp = System.Collections.Generic.KeyValuePair<string,
Is it possible to write generic class with one constructor which explicitly defines type
Is it possible to define a regex which will match every character except a
(How) is it possible to represent monads in Scala in a generic way (like
Possible Duplicate: Define a generic that implements the + operator I am recently working
Is it possible to define a generic type in C# that references itself? E.g.
I wonder if it is possible to define a generic C++ container that stores
Possible Duplicate: Define a method that has many (or infinite) arguments I have the
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
Is it possible to define a binding between an exchange (direct or topic) and

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.