Skip to content

[WIP] Safe initialization for Scala #4543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 83 commits into from

Conversation

liufengyun
Copy link
Contributor

@liufengyun liufengyun commented May 17, 2018

This is a joint attempt with @biboudis .

Currently, zero change to typer and types.

import scala.annotation.{ partial, init }


class Parent(x: Int) {
  var name: String = _
  var addr: String = _                       // warning: addr is not initialized

  val len = name.size                        // warning: name not initialized
  lazy val l1 = name.size                    // ok: l1 is lazy
  lazy val l2 = addr.size                    // warning: l2 is forced at L32 before `addr` is initialized

  val fun: Int => Int = n => n + list.size   // ok, fun is a partial value
  val bar = new Bar("bar", fun)              // ok, Bar accepts partial value
  bar.result                                 // warning: bar is a partial value

  val child = new Child(this)                // warning: `this` is partial, while full value expected
  List(5, 9).map(n => n + list.size)         // warning: partial value used as full value

  f(20)                                      // warning: list not initialized

  val list = List(1, 2, 3)

  if (x > 5) {
    name = "big"
    addr = "Lausanne"
  }
  else {
    name = "small"
  }

  val temp1 = l1                               // ok, name init
  val temp2 = l2                               // warning: addr not initialized

  // f is called during initialization, needs to be `private` or `final`.
  def f(m: Int) =
    m + list.size                              // warning: `f` is called at L19 before `list` is initialized

  List(1, 3, 5).map(n => n + list.size)        // ok, `this.list` already initialized
}


class Bar(val name: String, fun: (Int => Int) @partial) {
  def result = fun(20)
}

class Child(parent: Parent) {
  println(parent.name)
}

@liufengyun liufengyun force-pushed the safe-init branch 2 times, most recently from 77dadc4 to ac95f8c Compare May 23, 2018 11:35
@liufengyun liufengyun self-assigned this May 28, 2018
@liufengyun liufengyun force-pushed the safe-init branch 4 times, most recently from b22d79e to dd2c805 Compare June 6, 2018 15:29
@liufengyun liufengyun force-pushed the safe-init branch 2 times, most recently from b0eb8ad to 487ec8a Compare August 17, 2018 15:58
@liufengyun liufengyun force-pushed the safe-init branch 2 times, most recently from 1b8fc07 to 75db73e Compare September 21, 2018 08:32
@liufengyun liufengyun force-pushed the safe-init branch 5 times, most recently from 8ed55bd to afa1048 Compare October 4, 2018 20:09
@liufengyun
Copy link
Contributor Author

test performance please

@dottybot
Copy link
Member

dottybot commented Oct 4, 2018

performance test scheduled: 1 job(s) in queue, 0 running.

@dottybot
Copy link
Member

dottybot commented Oct 4, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/4543/ to see the changes.

Benchmarks is based on merging with master (42de57a)

@liufengyun liufengyun force-pushed the safe-init branch 5 times, most recently from 436931e to 045f7f3 Compare October 8, 2018 11:56
@liufengyun
Copy link
Contributor Author

test performance please

@dottybot
Copy link
Member

dottybot commented Oct 8, 2018

performance test scheduled: 2 job(s) in queue, 1 running.

@dottybot
Copy link
Member

dottybot commented Oct 8, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/4543/ to see the changes.

Benchmarks is based on merging with master (7463afe)

@liufengyun
Copy link
Contributor Author

test performance please

@dottybot
Copy link
Member

dottybot commented Nov 9, 2018

performance test scheduled: 1 job(s) in queue, 1 running.

@dottybot
Copy link
Member

dottybot commented Nov 9, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/4543/ to see the changes.

Benchmarks is based on merging with master (a9029dc)

@liufengyun
Copy link
Contributor Author

Close for now, more research is required to completely remove the annotation requirement.

@liufengyun liufengyun closed this Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants