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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:59+00:00 2026-05-27T06:58:59+00:00

Consider such code (this is just example not real code): class Foo(url : String)

  • 0

Consider such code (this is just example not real code):

class Foo(url : String) extends Bar(url)
{
  def say() { println(url) }
}

It compiles and it works. With nonsense results “of course”. I am too newbie to judge, but for me it serves no purpose but confusion — by definition it is impossible that the argument of the constructor could be reachable directly in another method.

Could someone more experience in Scala could point out condition when it might work or make sense? Or confirm my suspicion this is flaw in current Scala compiler.

Update

class Bar(val Url : String)
{
}

Why “nonsense”. Because my will (no “var” and no “val” in Foo) was to just pass the argument, nothing else. So when I actually use the constructor argument it is just matter of what entity I use by mistake. When you write on purpose, you hit the jackpot each time, but if you don’t (i.e. you make a mistake in spelling), you use entities by random. It is not the code does not make sense, it is the computation just does not make sense, I could roll a dice as well. There is a method scope, and I don’t see a reason why there shouldn’t be constructor scope.

Update — workaround

So it seems this evil construct is really part of the language (by design). As UserUnknown and Tomasz Nurkiewicz suggested the only line of defense against making stupid typo is convention, however lower-upper case is not good. “A” and “a” differ a lot, but “U” and “u” not much. Introducing one special character (as Tomasz showed) is much better, because it is possible to visually detect fishy usage of constructor argument.

I will use “$” for “just-passing” constructor arguments, it is harder to type for me, and you don’t see this character too often in the code.

Thank you for the answers!

Why it is evil? Because implicit actions should be allowed explicitly by users — good examples are “dynamic” in C#, implicit conversions in Scala. And examples of breaking this rule which led to tons of problems are — implicit conversions to bool in C++, implicit constructors in C++, declaration by usage in Perl. And this particular case is very, very close to the mentioned perlism, in Perl finally there was change in interpreter to detect such misusages, why Scala repeated the same mistake? I wonder.

  • 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-27T06:59:00+00:00Added an answer on May 27, 2026 at 6:59 am

    It’s not a bug, it’s a feature. In fact, a really nice one. Need an example how useful it is? Here is how I use it with Spring and dependency injection via constructor:

    @Service
    class Foo @Autowired() (bar: Bar, jdbcOperations: JdbcOperations) {
    
        def serverTime() = bar.format(jdbcOperations.queryForObject("SELECT now()", classOf[Date]))
    
    }
    

    Equivalent code in Java:

    @Service
    public class Foo
    {
        private final Bar bar;
        private final JdbcOperations jdbcOperations;
    
        @Autowired
        public Foo(Bar bar, JdbcOperations jdbcOperations)
        {
            this.bar = bar;
            this.jdbcOperations = jdbcOperations;
        }
    
        public String serverTime()
        {
            return this.bar.format(this.jdbcOperations.queryForObject("SELECT now()", Date.class));
        }
    
    }
    

    Still not convinced?

    Short tutorial:

    class Foo(var x: Int, val y: Int, z: Int) {
      println(z)
      //def zz = z
    }
    

    x will become a variable with getters and setter. y will become an immutable variable and z will become an immutable variable only if zz method is uncommented. Otherwise it will remain a constructor argument. Neat!

    UPDATE: I see your point now! The following code works as expected by accessing url variable in base class:

    class Bar(val url)
    
    class Foo(_url : String) extends Bar(_url)
    {
      def say() { println(url) }
    }
    

    I agree, this is both ugly and is asking for trouble. In fact I once hit this problem myself when using Scala classes as Hibernate entities – I used constructor parameter instead of field in base class which caused duplicated field to be created: one in base class and one in derived class. I wouldn’t even notice but Hibernate was screaming at runtime that duplicated column mapping was defined.

    So I have to somewhat agree with you – this is somehow confusing and might be error-prone. This is the price you pay for “implicitness” and concise code.

    However note that no modified and val modifier before constructor argument are different. Without modified immutable field is created, while val additionally adds getter.

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

Sidebar

Related Questions

Consider this code: namespace foo {} class A { class B { }; friend
Consider the following code. public interface IFoo { } public class Bar { public
Consider: print $foo, AAAAAAAA, $foo, BBBBBBBB; Let's say I want to use this code
Problem I have such code var ls = src.iter.toList src.iter = ls.iterator (this is
If we consider Man In the Middle Attack; Can such an attack occur if
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider these classes. class Base { ... }; class Derived : public Base {
Consider the following ruby code test.rb: begin puts thisFunctionDoesNotExist x = 1+1 rescue Exception

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.