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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:05:51+00:00 2026-05-28T18:05:51+00:00

I am starting to learn Scala and I will do a simple cross compiler.

  • 0

I am starting to learn Scala and I will do a simple cross compiler.

I will support a small set of instructions like print.

Note: the code snippets are not tested or compiled.
Here is what I would do in JAVA.

public interface Compiler{
 String getPrintInstruction();
}

public class JavaCompiler implements Compiler{
 public String getPrintInstruction(){
  return "System.out.print(arg0);"
 }
}

public class ScalaCompiler implements Compiler{
 public String getPrintInstruction(){
  return "print(arg0);"
 }
}

Is the snippet below the correct “Scala way“?

trait Compiler {
  var printInstruction: String
}
class JavaCompiler extends Compiler {
  var printInstruction = "System.out.print(arg0);"
}
class ScalaCompiler extends Compiler {
  var printInstruction = "print(arg0);"
}

EDIT:

I will move my second question to a new thread.

  • 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-05-28T18:05:53+00:00Added an answer on May 28, 2026 at 6:05 pm

    For a 1:1 mapping, those vars should be changed to defs.

    trait Compiler {
      def printInstruction: String
    }
    
    class JavaCompiler extends Compiler {
      def printInstruction = "System.out.print(arg0);"
    }
    
    class ScalaCompiler extends Compiler {
      def printInstruction = "print(arg0);"
    }
    

    def declares a method. When you don’t provide an implementation, it becomes an abstract method.

    EDIT:

    The technique used here is a valid and useful technique. Alternatively you could use one of the following two techniques to model your problem.

    1) Discriminated unions. (aka sum types.)

    Refer to this excellent article to learn about this concept. This is how your example would probably look like when modeled this way:

    sealed trait Compiler {
      def printInstruction: String = this match {
        case JavaCompiler => "System.out.print(arg0);"
        case ScalaCompiler => "print(arg0);"
      }
    }
    
    case object JavaCompiler extends Compiler
    case object ScalaCompiler extends Compiler
    

    2) Type class pattern.

    Here is a great post by Daniel Sobral on this topic. You can dig up a few more by googling the terms type-class, pattern, Scala, implicits etc. This is how your code might look like if the problem’s modeled with type class pattern:

    trait Compiler[C] {
      def printInstruction(c: C): String
    }
    
    case object JavaCompiler
    
    implicit object JavaCompilerIsCompiler extends Compiler[JavaCompiler.type] {
      def printInstruction(c: JavaCompiler.type): String = "System.out.print(arg0);"
    }
    
    case object ScalaCompiler
    
    implicit object ScalaCompilerIsCompiler extends Compiler[ScalaCompiler.type] {
      def printInstruction(c: ScalaCompiler.type) = "print(arg0);"
    }
    

    For your problem, the original approach and the discriminated unions approach seem to be the best modeling solutions.

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

Sidebar

Related Questions

Im starting to learn scala and mongo , my IDE is intellij IDEA. I
I am just starting to learn scala, and perhaps I am a little ambitious.
I am starting to learn android development. I need to set my netbeans IDE
I am a Scala newbie, just starting to learn the language. I solved Problem
I’m starting to learn MVC3 and I’d like to know if it is advisable
Just starting to learn scala for a new project. Have got to the point
Just starting to learn scala.. I can't seem to figure out how to compile
I am starting to learn ruby on rails, like it so far. But I
Whilst starting to learn lisp, I've come across the term tail-recursive . What does
Im starting to learn RoR and i want to make my personal blog in

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.