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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:17:56+00:00 2026-05-31T15:17:56+00:00

I never understood it from the contrived unmarshalling and verbing nouns ( an AddTwo

  • 0

I never understood it from the contrived unmarshalling and verbing nouns ( an AddTwo class has an apply that adds two!) examples.

I understand that it’s syntactic sugar, so (I deduced from context) it must have been designed to make some code more intuitive.

What meaning does a class with an apply function give? What is it used for, and what purposes does it make code better (unmarshalling, verbing nouns etc)?

how does it help when used in a companion object?

  • 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-31T15:17:57+00:00Added an answer on May 31, 2026 at 3:17 pm

    Mathematicians have their own little funny ways, so instead of saying “then we call function f passing it x as a parameter” as we programmers would say, they talk about “applying function f to its argument x“.

    In mathematics and computer science, Apply is a function that applies
    functions to arguments.
    Wikipedia

    apply serves the purpose of closing the gap between Object-Oriented and Functional paradigms in Scala. Every function in Scala can be represented as an object. Every function also has an OO type: for instance, a function that takes an Int parameter and returns an Int will have OO type of Function1[Int,Int].

     // define a function in scala
     (x:Int) => x + 1
    
     // assign an object representing the function to a variable
     val f = (x:Int) => x + 1
    

    Since everything is an object in Scala f can now be treated as a reference to Function1[Int,Int] object. For example, we can call toString method inherited from Any, that would have been impossible for a pure function, because functions don’t have methods:

      f.toString
    

    Or we could define another Function1[Int,Int] object by calling compose method on f and chaining two different functions together:

     val f2 = f.compose((x:Int) => x - 1)
    

    Now if we want to actually execute the function, or as mathematician say “apply a function to its arguments” we would call the apply method on the Function1[Int,Int] object:

     f2.apply(2)
    

    Writing f.apply(args) every time you want to execute a function represented as an object is the Object-Oriented way, but would add a lot of clutter to the code without adding much additional information and it would be nice to be able to use more standard notation, such as f(args). That’s where Scala compiler steps in and whenever we have a reference f to a function object and write f (args) to apply arguments to the represented function the compiler silently expands f (args) to the object method call f.apply (args).

    Every function in Scala can be treated as an object and it works the other way too – every object can be treated as a function, provided it has the apply method. Such objects can be used in the function notation:

    // we will be able to use this object as a function, as well as an object
    object Foo {
      var y = 5
      def apply (x: Int) = x + y
    }
    
    
    Foo (1) // using Foo object in function notation 
    

    There are many usage cases when we would want to treat an object as a function. The most common scenario is a factory pattern. Instead of adding clutter to the code using a factory method we can apply object to a set of arguments to create a new instance of an associated class:

    List(1,2,3) // same as List.apply(1,2,3) but less clutter, functional notation
    
    // the way the factory method invocation would have looked
    // in other languages with OO notation - needless clutter
    List.instanceOf(1,2,3) 
    

    So apply method is just a handy way of closing the gap between functions and objects in Scala.

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

Sidebar

Related Questions

There is one thing I never understood about references and I hope that one
I've been using that kind of methods on and off but never really understood
So, I've never understood the explain of MySQL. I understand the gross concepts that
First of all I just want to say that I never understood so well
I must admit that I never understood what are the streams are all about-
I never clearly understood what an ABI is. Please don't point me to a
I've never really understood on how to do it. I want to where where
Not a big user of RegEx - never really understood them! However, I feel
I've used MySQL (via PHPMyAdmin) a lot before but never really understood half of
Just trying to understand that - I have never used it before. How is

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.