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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:12:18+00:00 2026-05-14T23:12:18+00:00

in Scala, if I want to implement a DSL, is there a way to

  • 0

in Scala, if I want to implement a DSL, is there a way to do the following:

I have an Object called “Draw” which contains the function def draw(d:Drawable)

how can I make it so that I can import the Object and call it outside the object like:

draw ball

if ball extends the Drawable trait? The problem is that I want to use draw in a kind of infix notation, but I dont want to qualify the function draw by denoting it’s implementing class/object.

  • 1 1 Answer
  • 1 View
  • 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-05-14T23:12:19+00:00Added an answer on May 14, 2026 at 11:12 pm

    I quickly tried it out, but could quite make it work using an object. There I had to use draw(ball) instead of draw ball, as you wanted:

    
    Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).

    scala> trait Drawable{def doSomething}
    defined trait Drawable

    scala> object Draw {
    def draw(d:Drawable) = d.doSomething
    }
    defined module Draw

    scala> val ball = new Drawable{def doSomething = println("doing ball")}
    ball: java.lang.Object with Drawable = $anon$1@3a4ba4d6

    scala> import Draw._
    import Draw._

    scala> draw ball
    :11: error: missing arguments for method draw in object Draw;
    follow this method with `_' if you want to treat it as a partially applied function
    draw ball
    ^

    scala> draw(ball)
    doing ball

    However by defining Draw as a class, it did work:

    
    scala> trait Drawable{def doSomething: Unit}
    defined trait Drawable

    scala> class Draw {
    def draw(d:Drawable) = d.doSomething
    }
    defined class Draw

    scala>

    scala> val ball = new Drawable{def doSomething = println("doing ball")}
    ball: java.lang.Object with Drawable = $anon$1@36a06816

    scala> val d = new Draw
    d: Draw = Draw@7194f467

    scala> d draw ball
    doing ball

    I'm not completely sure why this doesn't work the same way with an object, might be a bug or perhaps that's specified behaviour. However I didn't have the time to look it up at the moment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to ask if there is a possible way to conditionally implement interfaces
I want to get a list of inner objects of a scala object. Sample
I have an app written in Scala and some of my team members want
I'm learning Scala, so this is probably pretty noob-irific. I want to have a
I have the following code, partly in Java, partly in Scala: Java: public interface
I've recently started learning scala, and I've come across the :: (cons) function, which
I want to implement some kind of message bus in one of my Scala
I am very new to Scala. I want to implement a generic matrix class
I'm learning Scala. I want to implement a subclass of Exception that gets a
I want to define lifting with implicits. Suppose we have a function A =>

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.