Skip to content

Instantly share code, notes, and snippets.

View SethTisue's full-sized avatar

Seth Tisue SethTisue

View GitHub Profile
scala> import scala.language.reflectiveCalls
import scala.language.reflectiveCalls
scala> type Structural = { def foo: Int; def foo_=(x: Int): Unit }
defined type alias Structural
scala> object O { var foo = 5 }
defined object O
scala> (O: Any).asInstanceOf[Structural].foo = 10
@SethTisue
SethTisue / scalawags-38.md
Last active July 3, 2016 09:17
Scalawags #38: The Sound of Dotty
@SethTisue
SethTisue / scalawags-37.md
Last active January 7, 2016 04:30
Scalawags #37: Holiday Special 2015/6
@SethTisue
SethTisue / implicit.scala
Created December 24, 2015 15:39
make your implicit classes `val` parameters `private`
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).
scala> implicit class Foo(val x: Int) { def foo = x * 2 }
defined class Foo
scala> 3.x
res0: Int = 3
scala> :reset
Resetting interpreter state.
# repo is https://github.com/SethTisue/Project-Euler
% sbt
[info] Loading global plugins from /Users/tisue/.sbt/0.13/plugins
[info] Updating {file:/Users/tisue/.sbt/0.13/plugins/}global-plugins...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Loading project definition from /Users/tisue/euler/project
[info] Updating {file:/Users/tisue/euler/project/}euler-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
@SethTisue
SethTisue / scalawags-35.md
Last active November 21, 2015 15:27
Scalawags #35
@SethTisue
SethTisue / ones.scala
Created November 5, 2015 12:38
Parade of ones vs. circle of ones
scala> def one = { println("hey!"); 1 }
one: Int
scala> val endlessParadeOfOnes = Stream.continually(one)
hey!
endlessParadeOfOnes: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> endlessParadeOfOnes.take(5).sum
hey!
hey!

Ichoran
Anyway, point being that all lazy vals have a bit of performance overhead, local and nonlocal. Hand-coding will generally be better if you can keep everything on the stack.

viktorklang Oct 12 19:32
IMHO lazy val should be non-thread safe and @volatile lazy val should be thread safe...

Ichoran Oct 12 19:40
@viktorklang - Yeah, that'd be nice. Or at least allow a @nonvolatile annotation to opt-in to the thread-unsafe version.

Trait with superaccessor

here's T.scala:

trait T1 {
  def x = 3
}
trait T2 extends T1 {
  override def x = super.x + 1

}