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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:31:59+00:00 2026-05-23T03:31:59+00:00

This is a trivial question, I was just wondering if I can use something

  • 0

This is a trivial question, I was just wondering if I can use something Scala-ish here. I have 2 different classes (Server1 and Server2) both implement a common interface/trait (with methods .A() and .B()).

This obviously confuses the compiler:

var server = null
if(cond) server=new Server1 else server=new Server2
server.A() //or server.B()

I was mainly curious if I could use Scala’s Option to get around this in a neat way. Thanks.

  • 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-23T03:31:59+00:00Added an answer on May 23, 2026 at 3:31 am

    Type annotations can always be added:

    var server: TraitOrInterface = null
    

    However, since vars are somewhat icky…

    val server = if (cond) {
       new Server1()
    } else {
       new Server2()
    }
    

    In the second example, Scala should be able to unify types. (I am fairly certain there are some situations it can’t unify — or where it doesn’t unify quite as desired, but give it a shot before falling back to type annotations, which can be added as per the first example.)

    REPL demonstration:

    class X
    trait Y
    class A extends X with Y {}
    class B extends X with Y {}
    val uni = if (true) new A() else new B()
    uni
    >> res3: X with Y = A@17b8cf0
    

    Happy coding.


    Structural typing example, the type alias is for convenience but technically not required.

    class Cat { def speak() = "meow" }
    class Dog { def speak() = "woof" }
    type ThingThatSpeaks = { def speak(): String }
    
    val speaker : ThingThatSpeaks = if (true /* smart */) new Cat() else new Dog()
    speaker
    >> res4: ThingThatSpeaks = Cat@2893fc
    speaker.speak()
    >> res5: String = meow
    

    Note that the type annotation was required, otherwise…

    val speaker = if (true /* smart */) new Cat() else new Dog()
    speaker
    >> res6: ScalaObject = Cat@f0ac6e
    speaker.speak()
    >> error: value speak is not a member of ScalaObject
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is just a trivial question of what convention you suggest. Recently, I have
Yes, it is a trivial question. I was just wondering. Does the framework have
This is probably a trivial question for most but here goes... I am using
I'm new in the scala world, so excuse my trivial question. :) I just
This might be a trivial one, but I kind of stuck here. I have
This is very trivial question regarding the use of a constructor in C++. I
I realise this is a trivial question but I don't have access to a
This is a stupidly trivial question, but I can't seem to find a proper
I know this seams a trivial question, but how can I disable the annoying
Wondering if anyone can help me with this annoying but trivial (in terms of

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.