Skip to content

Instantly share code, notes, and snippets.

@cvogt
Last active February 22, 2017 13:51
Show Gist options
  • Save cvogt/65e33ed1f9de93c151e0ff145f8e74fb to your computer and use it in GitHub Desktop.
Save cvogt/65e33ed1f9de93c151e0ff145f8e74fb to your computer and use it in GitHub Desktop.

Revisions

  1. cvogt revised this gist Feb 22, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions defaults-and-copy-ideas.scala
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ case class Baz(bar: Bar)
    val b = Baz( Bar( "world" ) )
    assert(
    b.copy( bar = b.bar.copy( str = "hello, " ++ b.bar.str ) ) // old: having to repeat repeat b and b.bar
    == b.copy( bar ~= _.copy( str ~= "hello, " ++ _ ) ) // new: short version via ~= operator for modification
    == b.copy( bar _= _.copy( str _= "hello, " ++ _ ) ) // new: short version via ~= operator for modification (alt syntax ~=)
    )
    // new short version desugars to this (using access to default values):
    b.copy( bar = b.copy.bar.copy( str = "hello, " ++ b.copy.bar.copy.str ) )
    @@ -22,6 +22,6 @@ b.copy( bar = b.copy.bar.copy( str = "hello, " ++ b.copy.bar.copy.str ) )
    class Foo{ def str = "world" }
    assert(
    new Foo{ override str = "hello, " ++ super.str }.str // having to repeat `str` in override
    == new Foo{ override str ~= "hello, " ++ _ }.str // new: modifying function based on parent value
    == new Foo{ override str _= "hello, " ++ _ }.str // new: modifying function based on parent value (alt syntax ~=)
    )

  2. cvogt revised this gist Feb 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion defaults-and-copy-ideas.scala
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ assert( Bar.apply.str == "test" ) // same for case classes via .apply method
    case class Baz(bar: Bar)
    val b = Baz( Bar( "world" ) )
    assert(
    b.copy( bar = b.bar.copy( str = "hello, " ++ b.bar.str ) ) // old: having to repeat repeat b.bar
    b.copy( bar = b.bar.copy( str = "hello, " ++ b.bar.str ) ) // old: having to repeat repeat b and b.bar
    == b.copy( bar ~= _.copy( str ~= "hello, " ++ _ ) ) // new: short version via ~= operator for modification
    )
    // new short version desugars to this (using access to default values):
  3. cvogt revised this gist Feb 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion defaults-and-copy-ideas.scala
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ assert( foo.str == "test" ) // new: named access to default values
    case class Bar(str: String = "test")
    assert( Bar.apply.str == "test" ) // same for case classes via .apply method

    // REPRTIION-FREE .copy (aka lenses light)
    // REPETIION-FREE .copy (aka lenses light)

    case class Baz(bar: Bar)
    val b = Baz( Bar( "world" ) )
  4. cvogt revised this gist Feb 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion defaults-and-copy-ideas.scala
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ assert( foo.str == "test" ) // new: named access to default values
    case class Bar(str: String = "test")
    assert( Bar.apply.str == "test" ) // same for case classes via .apply method

    // REPITIION-FREE .copy (aka lenses light)
    // REPRTIION-FREE .copy (aka lenses light)

    case class Baz(bar: Bar)
    val b = Baz( Bar( "world" ) )
  5. cvogt renamed this gist Feb 22, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. cvogt created this gist Feb 22, 2017.
    27 changes: 27 additions & 0 deletions defaults-and-copy-ideas
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    // DEFAULT VALUES

    def foo(str: String = "test") = ???
    assert( foo.str == "test" ) // new: named access to default values

    case class Bar(str: String = "test")
    assert( Bar.apply.str == "test" ) // same for case classes via .apply method

    // REPITIION-FREE .copy (aka lenses light)

    case class Baz(bar: Bar)
    val b = Baz( Bar( "world" ) )
    assert(
    b.copy( bar = b.bar.copy( str = "hello, " ++ b.bar.str ) ) // old: having to repeat repeat b.bar
    == b.copy( bar ~= _.copy( str ~= "hello, " ++ _ ) ) // new: short version via ~= operator for modification
    )
    // new short version desugars to this (using access to default values):
    b.copy( bar = b.copy.bar.copy( str = "hello, " ++ b.copy.bar.copy.str ) )

    // MODIFYING OVERRIDES

    class Foo{ def str = "world" }
    assert(
    new Foo{ override str = "hello, " ++ super.str }.str // having to repeat `str` in override
    == new Foo{ override str ~= "hello, " ++ _ }.str // new: modifying function based on parent value
    )