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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:50:39+00:00 2026-05-30T13:50:39+00:00

I have been reading about methods and functions in Scala. Jim’s post and Daniel’s

  • 0

I have been reading about methods and functions in Scala. Jim’s post and Daniel’s complement to it do a good job of explaining what the differences between these are. Here is what I took with me:

  • functions are objects, methods are not;
  • as a consequence functions can be passed as argument, but methods can not;
  • methods can be type-parametrised, functions can not;
  • methods are faster.

I also understand the difference between def, val and var.

Now I have actually two questions:

  1. Why can’t we parametrise the apply method of a function to parametrise the function? And
  2. Why can’t the method be called by the function object to run faster? Or the caller of the function be made calling the original method directly?

Looking forward to your answers and many thanks in advance!

  • 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-30T13:50:41+00:00Added an answer on May 30, 2026 at 1:50 pm

    1 – Parameterizing functions.

    It is theoretically possible for a compiler to parameterize the type of a function; one could add that as a feature. It isn’t entirely trivial, though, because functions are contravariant in their argument and covariant in their return value:

    trait Function1[+T,-R] { ... }
    

    which means that another function that can take more arguments counts as a subclass (since it can process anything that the superclass can process), and if it produces a smaller set of results, that’s okay (since it will also obey the superclass construct that way). But how do you encode

    def fn[A](a: A) = a
    

    in that framework? The whole point is that the return type is equal to the type passed in, whatever that type has to be. You’d need

    Function1[ ThisCanBeAnything, ThisHasToMatch ]
    

    as your function type. "This can be anything" is well-represented by Any if you want a single type, but then you could return anything as the original type is lost. This isn’t to say that there is no way to implement it, but it doesn’t fit nicely into the existing framework.

    2 – Speed of functions.

    This is really simple: a function is the apply method on another object. You have to have that object in order to call its method. This will always be slower (or at least no faster) than calling your own method, since you already have yourself.

    As a practical matter, JVMs can do a very good job inlining functions these days; there is often no difference in performance as long as you’re mostly using your method or function, not creating the function object over and over. If you’re deeply nesting very short loops, you may find yourself creating way too many functions; moving them out into vals outside of the nested loops may save time. But don’t bother until you’ve benchmarked and know that there’s a bottleneck there; typically the JVM does the right thing.

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

Sidebar

Related Questions

I have been reading about Scala for a while and even wrote some small
I have been reading up on using webrequests and post and get methods to
I have been reading about the differences between Table Variables and Temp Tables and
I have been reading about collision detection in games on stackoverflow and other sites.
I have been reading about the continuation passing style programming technique (C# 3.0 implementation).
I have been reading about Quicksort and found that sometimes it' s referred to
i have been reading about multiple inheritance What is the exact problem with multiple
I am using Codeigniter to develop an application, and have been reading bits about
I have been reading a lot about C++ casting and I am starting to
I have been hearing and reading about Agile for years. I own a book

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.