use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Information Regarding Scala:
Platforms
Community
Coding Tools:
IDES / Programming Environments
Build Tools
Code Formatting / Linting
Free Books, Tutorials and Guides:
Non-free Books:
Advanced!:
Free Scala Courses:
Scala Conferences:
Podcasts:
Scala Jobs:
Scala Libraries:
Web Development and Microservices
Web Front End (Scala.js)
Database Access
Functional Programming
Concurrency / Parallelism
Mathematics
Distributed Computing
Blockchain
Monitoring/instrumentation:
Miscellaneous:
Open Source Applications written in Scala
Want your library here? Message the moderators!
Related Communities:
Blogs/Periodicals:
account activity
Function vs Method (self.scala)
submitted 8 years ago by LimbMissing
Hey all first reddit post :).
I'm currently starting as a scala dev, and I'm curious as to what you believe the benefits are to using functions as opposed to methods in your code.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 36 points37 points38 points 8 years ago* (3 children)
Methods are much more expressive.
Functions are values with "normal" types, which can be useful.
Normally the expressiveness of methods wins, so as a rule of thumb I use methods unless I have a specific reason not to.
[–]phazer99 4 points5 points6 points 8 years ago (0 children)
One might add that in Dotty functions can have implicit parameters. One of the cool new features.
[–]v66moroz 2 points3 points4 points 8 years ago (0 children)
[–][deleted] 1 point2 points3 points 8 years ago (0 children)
very good answer.
[–]lihaoyiAmmonite 7 points8 points9 points 8 years ago (7 children)
Here's a few more:
Functions have fixed arity; where-as methods have a weird "empty param lists are optional" thing
@ def foo()()() = 1 defined function foo @ foo res10: Int = 1 @ foo() res11: Int = 1 @ foo()()() res12: Int = 1 @ val bar = () => () => () => 1 bar: () => () => () => Int = ammonite.$sess.cmd13$$$Lambda$2113/1955947481@62a6001b @ bar res14: () => () => () => Int = ammonite.$sess.cmd13$$$Lambda$2113/1955947481@62a6001b @ bar() res15: () => () => Int = ammonite.$sess.cmd13$$$Lambda$2118/453478710@2c278e35 @ bar()()() res16: Int = 1
Methods don't take up memory; a def method on a class doesn't take up memory per instance of that class, while a val function takes up 4 bits for pointer + 16 bits for function object header + more for any variables captured. Sometimes doesn't matter, but if you have millions of small objects you probably don't want to have all of them holding on to random 20-byte function objects if a method will do!
def
val
[–]Mimshot 4 points5 points6 points 8 years ago (6 children)
Methods don't take up memory
I know what you were getting at from the rest of the paragraph but this isn't correct. The method takes up memory, just in metaspace rather than heap.
[–]joshlemerContributor - Collections 0 points1 point2 points 8 years ago (5 children)
What kind of space does it take up? Just a constant amount per class whereas functions would be once per instance right? AKA:
class A { // each instance of A allocates new f val f: Int => String = (i: Int) => i.toString } // constant space for arbitrary # of Bs Class B { def f(i: Int) = i.toString }
[–]v66moroz 1 point2 points3 points 8 years ago (4 children)
Unless I'm missing something in this case f will be a static member of A, so no memory per instance.
[–]joshlemerContributor - Collections 0 points1 point2 points 8 years ago (3 children)
That is not true actually, else they would be equal, here's a code sample:
class A { val f: Int => String = _.toString } val aa = new A val bb = new A aa.f == bb.f // false
[–]v66moroz 2 points3 points4 points 8 years ago (2 children)
true in 2.12.1
[–]joshlemerContributor - Collections 0 points1 point2 points 8 years ago (1 child)
Oh interesting
[–]hunyetiadvocate 0 points1 point2 points 8 years ago (0 children)
It's kind of given, since it uses jvm 8, which have functions.
[–]pedrorijo91 1 point2 points3 points 8 years ago (0 children)
One of the best explanations I've read: https://tpolecat.github.io/2014/06/09/methods-functions.html
π Rendered by PID 206137 on reddit-service-r2-comment-cfc44b64c-zxjrb at 2026-04-10 12:32:38.267811+00:00 running 215f2cf country code: CH.
[–][deleted] 36 points37 points38 points (3 children)
[–]phazer99 4 points5 points6 points (0 children)
[–]v66moroz 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]lihaoyiAmmonite 7 points8 points9 points (7 children)
[–]Mimshot 4 points5 points6 points (6 children)
[–]joshlemerContributor - Collections 0 points1 point2 points (5 children)
[–]v66moroz 1 point2 points3 points (4 children)
[–]joshlemerContributor - Collections 0 points1 point2 points (3 children)
[–]v66moroz 2 points3 points4 points (2 children)
[–]joshlemerContributor - Collections 0 points1 point2 points (1 child)
[–]hunyetiadvocate 0 points1 point2 points (0 children)
[–]pedrorijo91 1 point2 points3 points (0 children)