Idea Transcript
POPULAR - ALL - RANDOM - USERS | ASKREDDIT - WORLDNEWS - VIDEOS - FUNNY - TODAYILEARNED - PICS - GAMING - MOVIES - NEWS - GIFS - MILDLYINTERESTING - AWW - SHOWERTHOUGHTS - TELEVISION - JOKES - SCIENCE - OLDSCHOOLCOO MORE »
Scala
Want to join? Log in or sign up in seconds. |
comments
search
Welcome to Reddit.
this post was submitted on 29 May 2017
Where a community about your favorite things is waiting for you. BECOME A REDDITOR
5
5 points (86% upvoted) shortlink: https://redd.it/6dzeif
and subscribe to one of thousands of communities.
username
transient parameters in a class constructor ? (self.scala ) submitted 9 months ago by kodifies
password
remember me
login
reset password
now I could be totally wrong here but say we have a class class (z: int, y: foo) z and y automagically become fields (if that is the right Scala terminology) of the class however suppose z and y are only ever used in the constructor to actually set up class members z isn't too much to worry about, but y is keeping at least a reference alive or if passing by value (need to find out!) then a blob of memory how can I be sure that y is properly disposed of after the constructor is done? 8 comments
share
save
hide
report
all 8 comments sorted by: best
Submit a new link Want to add to the discussion? Post a comment!
Submit a new text post
CREATE AN ACCOUNT
scala subscribe
[–] feral_claire 2 points 9 months ago
Without adding val or using a case class the constructor parameters so not become fields automatically. They just live on the stack so they will disappear as soon as the constructor returns.
Information Regarding Scala: Official Website Try Scala Online with Scastie or ScalaFiddle Contribute to Scala Scala Center (not-for-profit scala org) Documentation StackOverflow Recent Questions
Scala uses java call semantics so objects are passed by reference value. permalink
embed save
[–] naftoligug 2 points 9 months ago
The best way to tell what's happening is to use javap on the class.
16,486 readers
Platforms
I believe in this case:
JVM (main language site) Scala.js Scala Native Android
class C(x: Int) { val y = x + 1 } x does not become a field at all (because it's never used later), while in this case:
Community
class C(x: Int) { def y = x + 1 }
Official Scala Gitter Channel Official Scala User Forum Unofficial Scala Discord
x becomes a private field so it can be accessed whenever y is computed. So in other words, it becomes a field if there's a need, otherwise not (unless you specify, like with the val keyword or making it a case class).
Coding Tools:
permalink
IDES / Programming Environments
embed save
IntelliJ with Scala Plugin ScalaIDE Ensime Emacs,Atom,Vim,VSC,Sublime Ammonite Scripting, REPL, System shell
[–] Daxten 1 point 9 months ago
hey, those parameters are used only in the constructor, for example:
Build Tools SBT (Most used in community) Mill (quite new) CBT (quite new) Maven, Gradle (from Java ecosystem)
class Point(x: Int, y: Int) val p = new Point(1, 3) p.x // does not compile to make these public members you have to add "val" before the parameter. So if you don't add "val" the gc will clean these up permalink
embed save
Code Formatting / Linting Scalafmt Wartremover Free Books, Tutorials and Guides: Scala Exercises Twitter's Scala School Scala Tutorial Programming in Scala, First Edition Hands-on Scala.js Underscore.io books (many free books) 47Degrees Scala Exercises The Neophyte's Guide to Scala
[–] kodifies [S] 1 point 9 months ago
ah, my bad, yes I see exactly what you mean ! permalink
embed save
parent
[–] kevin_meredith 1 point 9 months ago
How could one show that x and y are garbage collected? permalink
embed save
[–] lihaoyi
Non-free Books:
parent
Programming in Scala, Third Edition Programming Scala (O'Reilley)
Ammonite 2 points 9 months ago*
Advanced!:
Easy: replace them with Array[Int] s, create a few thousand points each with new Array[Int](1000000) s in them, and see if you run out of memory or not. If they get GCed, the JVM can easily hold a few hundred millions small Point s with nothing inside. There'll be some churn and pauses from collection garbage, but the program will go on.
Functional Programming in Scala (Manning) Mastering Advanced Scala Free Scala Courses: Functional Programming Principles in Scala Functional Program Design in Scala Parallel Programming Big Data Analysis with Scala and Spark Introduction to Programming with Dependent Types in Scala (advanced)
If they don't get GCed, a few thousand and you'll be OOM EDIT: and if you don't replace them with Array[Int] s, Int s aren't objects and don't create garbage anyway. They'll just take up space on the existing Point objects and make each one 4 or 8 bytes bigger. If you want to see how many fields are on Point , you can use javap -c on the compiled classfile permalink
embed save
parent
Scala Conferences:
[–] Daxten 1 point 9 months ago
Typelevel Summits (Misc.) Scala by the Bay (USA) flatMap (Norway) Scala Up North (Canada) Scala Days (USA, Europe) Scala World (UK) Scala Swarm (Portugal) Scala.io (France) Scalar (Central Europe) Scala Sphere (Poland)
good question and I can't actually answer that (but I bet someone else can) since there is no reference to them they SHOULD get garbage collected (you could add one, e.g. def getX() = x, but the compiler should be able to optimize and see if you use them later or not) permalink
embed save
parent
[–] Milyardo 1 point 9 months ago
Override finalize on the argument to print a line when it is GC'd. permalink
embed save
Podcasts:
parent
Corecursive Scala Jobs: Functional Jobs Lightbend Customer Postings Functional Works Scala Jobs Scala Libraries: Scaladex: The Scala Library Index Web Development and Microservices Play Akka HTTP Lagom Sttp (HTTP Client) http4s Finch Udash - Frontend and Backend Lift Scalatra Skinny Vert.x Sangria - GraphQL Web Front End (Scala.js) ScalaJS-React Binding.scala React4s Databases Doobie (RDBMS) Quill (RDBMS + Cassandra) Phantom (Cassandra) Slick (RDBMS) ScalikeJDBC (RDBMS) ReactiveMongo (MongoDB) Elastic4s (ElasticSearch) Relate (RDBMS) Functional Programming Typelevel large functional programming community Scalaz Shapeless Cats Freestyle Concurrency / Parallelism Akka Monix Mathematics Spire Distributed Computing Apache Spark Apache Flink Apache Gearpump Apache Kafka . Miscellaneous: Scodec Open Source Applications written in Scala Lichess (chess) Graphcool (GraphQL) Apache Kafka Gitbucket Crypto Coin Alerts Want your library here? Message the moderators! Related Communities: /r/ScalaEs (Spanish) /r/ScalaStudyGroup /r/ScalaConferenceVideos /r/Play /r/Akka /r/ApacheSpark /r/ApacheKafka Blogs: Official blog Lihaoyi SoftwareMill tpolecat Typelevel 10 years
MODERATORS mbseid ranci joshlemer
Apache Flink - Enthusiast
AutoModerator about moderation team » <
>
discussions in r/scala
X
8 · 3 comments
Scala: try-with-resources
about
help
apps & tools