Quantcast

lazy var

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

lazy var

mustaghattack
Just wondering why lazy can only apply to val and not var ?

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lazy var

tmorris
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mustaghattack wrote:
> Just wondering why lazy can only apply to val and not var ?
>
> Thanks
Write a non-trivial program using this hypothetical language feature.
Try to reason about this program. Observe chaos.
Conclude: Laziness and side-effects -> spaghetti brain.

- --
Tony Morris
http://tmorris.net/

S, K and I ought to be enough for anybody.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJE2m5mnpgrYe6r60RAoi7AJ4weV7grbVTFSQNzu8DyBS076RkawCgyxzo
JfJTB0w+SUCEI1Jj9SgFi6Y=
=5XiV
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lazy var

csar
java.lang.ThreadLocal<T> T initialValue()

but you have a point, I always use this like a "val"

On Thu, Nov 6, 2008 at 11:03 PM, Tony Morris <[hidden email]> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mustaghattack wrote:
> Just wondering why lazy can only apply to val and not var ?
>
> Thanks
Write a non-trivial program using this hypothetical language feature.
Try to reason about this program. Observe chaos.
Conclude: Laziness and side-effects -> spaghetti brain.

- --
Tony Morris
http://tmorris.net/

S, K and I ought to be enough for anybody.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJE2m5mnpgrYe6r60RAoi7AJ4weV7grbVTFSQNzu8DyBS076RkawCgyxzo
JfJTB0w+SUCEI1Jj9SgFi6Y=
=5XiV
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lazy var

Ricky Clarkson
In reply to this post by mustaghattack
Lazy Var Puzzler #1

lazy var x = { println("Do you see me?")

               5 }
x = { println("Or me?")
      10 }

What gets printed?  Is this useful?

2008/11/6 mustaghattack <[hidden email]>:
>
> Just wondering why lazy can only apply to val and not var ?
>
> Thanks
> --
> View this message in context: http://www.nabble.com/lazy-var-tp20365847p20365847.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lazy var

mustaghattack
What about this :

1- Simple domain

abstract class Person( var id : String ) {
  var name : String
}

abstract class Book( var isbn : String ) {
  var author : Person
}

2- Simple repositories

trait PersonRepository {
  def findById( id : String ) : Person
}

trait BookRepository {
  def findByIsbn( isbn : String ) : Book
}

trait BookService {
  def findByIsbn( isbn : String ) : xml.Node

class XmlBookRepository( personRepository : PersonRepository, bookService : BookService ) {
  def findByIsbn( isbn : String ) : Book = {
     val bookNode = bookService.findByIsbn( isbn )
     new Book( ( bookNode attribute "isbn" get ).text )  {
         // load lazily the author
         override lazy var author = personRepository.findById( ( bookNode \ "author" ).text )
     }
  }
}

// plus factories and their implementation
trait RepositoryFactory( serviceFactory : ServiceFactory ) ...

3- Now you can fetch entities, and update them

// get a book
val repositoryFactory = new XmlRepositoryFactory( ... )
val zeBook = repositoryFactory.bookRepository.findByIsbn( "123-0-123-12333-2" )
println( zeBook.author.name ) // fetch lazily the author, but you don't have to ...

// update author
val anotherAuthor = repository.personRepository.findById( "234" )
zeBook.author = anotherAuthor
println( zeBook.author ) // print anotherAuthor
// of course if the first println didn't occur, we don't want now to fetch the author in db with anotherAuthor

// and save it
// repositoryFactory.bookRepository.save( zeBook )




Ricky Clarkson wrote
Lazy Var Puzzler #1

lazy var x = { println("Do you see me?")
               5 }
x = { println("Or me?")
      10 }

What gets printed?  Is this useful?

2008/11/6 mustaghattack <biethb@gmail.com>:
>
> Just wondering why lazy can only apply to val and not var ?
>
> Thanks
> --
> View this message in context:
http://www.nabble.com/lazy-var-tp20365847p20365847.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lazy var

Ricky Clarkson
override lazy var author = personRepository.findById( ( bookNode \ "author" ).text )

If you have that lazy var there, and you assign to it before you read it, the personRepository.findById call will never occur.  This would seem surprising, at a glance.  Do you disagree?

2008/11/7 mustaghattack <[hidden email]>

What about this :

1- Simple domain

abstract class Person( var id : String ) {
 var name : String
}

abstract class Book( var isbn : String ) {
 var author : Person
}

2- Simple repositories

trait PersonRepository {
 def findById( id : String ) : Person
}

trait BookRepository {
 def findByIsbn( isbn : String ) : Book
}

trait BookService {
 def findByIsbn( isbn : String ) : xml.Node

class XmlBookRepository( personRepository : PersonRepository, bookService :
BookService ) {
 def findByIsbn( isbn : String ) : Book = {
    val bookNode = bookService.findByIsbn( isbn )
    new Book( ( bookNode attribute "isbn" get ).text )  {
        // load lazily the author
        override lazy var author = personRepository.findById( ( bookNode \
"author" ).text )
    }
 }
}

// plus factories and their implementation
trait RepositoryFactory( serviceFactory : ServiceFactory ) ...

3- Now you can fetch entities, and update them

// get a book
val repositoryFactory = new XmlRepositoryFactory( ... )
val zeBook = repositoryFactory.bookRepository.findByIsbn(
"123-0-123-12333-2" )
println( zeBook.author.name ) // fetch lazily the author, but you don't have
to ...

// update author
val anotherAuthor = repository.personRepository.findById( "234" )
zeBook.author = anotherAuthor
println( zeBook.author ) // print anotherAuthor
// of course if the first println didn't occur, we don't want now to fetch
the author in db with anotherAuthor

// and save it
// repositoryFactory.bookRepository.save( zeBook )





Ricky Clarkson wrote:
>
> Lazy Var Puzzler #1
>
> lazy var x = { println("Do you see me?")
>                5 }
> x = { println("Or me?")
>       10 }
>
> What gets printed?  Is this useful?
>
> 2008/11/6 mustaghattack <[hidden email]>:
>>
>> Just wondering why lazy can only apply to val and not var ?
>>
>> Thanks
>> --
>> View this message in context:
> http://www.nabble.com/lazy-var-tp20365847p20365847.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>

--
View this message in context: http://www.nabble.com/lazy-var-tp20365847p20379966.html
Sent from the Scala - User mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lazy var

mustaghattack
I don't find it surprising. It's exactly the kind of behaviour I'm looking for. Why would you fetch the original author when you just want to change it ?

Ricky Clarkson wrote
 override lazy var author = personRepository.findById( ( bookNode \ "author"
).text )

If you have that lazy var there, and you assign to it before you read it,
the personRepository.findById call will never occur.  This would seem
surprising, at a glance.  Do you disagree?

2008/11/7 mustaghattack <biethb@gmail.com>

>
> What about this :
>
> 1- Simple domain
>
> abstract class Person( var id : String ) {
>  var name : String
> }
>
> abstract class Book( var isbn : String ) {
>  var author : Person
> }
>
> 2- Simple repositories
>
> trait PersonRepository {
>  def findById( id : String ) : Person
> }
>
> trait BookRepository {
>  def findByIsbn( isbn : String ) : Book
> }
>
> trait BookService {
>  def findByIsbn( isbn : String ) : xml.Node
>
> class XmlBookRepository( personRepository : PersonRepository, bookService :
> BookService ) {
>  def findByIsbn( isbn : String ) : Book = {
>     val bookNode = bookService.findByIsbn( isbn )
>     new Book( ( bookNode attribute "isbn" get ).text )  {
>         // load lazily the author
>         override lazy var author = personRepository.findById( ( bookNode \
> "author" ).text )
>     }
>  }
> }
>
> // plus factories and their implementation
> trait RepositoryFactory( serviceFactory : ServiceFactory ) ...
>
> 3- Now you can fetch entities, and update them
>
> // get a book
> val repositoryFactory = new XmlRepositoryFactory( ... )
> val zeBook = repositoryFactory.bookRepository.findByIsbn(
> "123-0-123-12333-2" )
> println( zeBook.author.name ) // fetch lazily the author, but you don't
> have
> to ...
>
> // update author
> val anotherAuthor = repository.personRepository.findById( "234" )
> zeBook.author = anotherAuthor
> println( zeBook.author ) // print anotherAuthor
> // of course if the first println didn't occur, we don't want now to fetch
> the author in db with anotherAuthor
>
> // and save it
> // repositoryFactory.bookRepository.save( zeBook )
>
>
>
>
>
> Ricky Clarkson wrote:
> >
> > Lazy Var Puzzler #1
> >
> > lazy var x = { println("Do you see me?")
> >                5 }
> > x = { println("Or me?")
> >       10 }
> >
> > What gets printed?  Is this useful?
> >
> > 2008/11/6 mustaghattack <biethb@gmail.com>:
> >>
> >> Just wondering why lazy can only apply to val and not var ?
> >>
> >> Thanks
> >> --
> >> View this message in context:
> > http://www.nabble.com/lazy-var-tp20365847p20365847.html
> >> Sent from the Scala - User mailing list archive at Nabble.com.
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/lazy-var-tp20365847p20379966.html
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lazy var

James Iry-2
In reply to this post by mustaghattack
It's not part of the language so you write your own and decide if it's too confusing to be useful.

  class LazyVar[T](init : => T) {
    private var thunk : (() => T) = {() => {thunk = {() => init}; thunk()}}
   
    def ! = synchronized { thunk() }
    def :=(newVal : => T) = synchronized { thunk = {() => {thunk = {() => newVal}; thunk()}}}
   
    def morph(f : T => T) = synchronized { val oldThunk = thunk ; thunk = { () => { thunk = {() => f(oldThunk())}; thunk()}}}                        
  }
  object LazyVar {
    def apply[T](init : => T) = new LazyVar({init})
  }

scala> val x = LazyVar(1)
x: LazyVar[Int] = LazyVar@5354a

scala> x := {println("assigning 2"); 2}

scala> x := {println("assigning 3"); 3}

scala> x!
assigning 3
res16: Int = 3




On Fri, Nov 7, 2008 at 4:48 AM, mustaghattack <[hidden email]> wrote:

What about this :

1- Simple domain

abstract class Person( var id : String ) {
 var name : String
}

abstract class Book( var isbn : String ) {
 var author : Person
}

2- Simple repositories

trait PersonRepository {
 def findById( id : String ) : Person
}

trait BookRepository {
 def findByIsbn( isbn : String ) : Book
}

trait BookService {
 def findByIsbn( isbn : String ) : xml.Node

class XmlBookRepository( personRepository : PersonRepository, bookService :
BookService ) {
 def findByIsbn( isbn : String ) : Book = {
    val bookNode = bookService.findByIsbn( isbn )
    new Book( ( bookNode attribute "isbn" get ).text )  {
        // load lazily the author
        override lazy var author = personRepository.findById( ( bookNode \
"author" ).text )
    }
 }
}

// plus factories and their implementation
trait RepositoryFactory( serviceFactory : ServiceFactory ) ...

3- Now you can fetch entities, and update them

// get a book
val repositoryFactory = new XmlRepositoryFactory( ... )
val zeBook = repositoryFactory.bookRepository.findByIsbn(
"123-0-123-12333-2" )
println( zeBook.author.name ) // fetch lazily the author, but you don't have
to ...

// update author
val anotherAuthor = repository.personRepository.findById( "234" )
zeBook.author = anotherAuthor
println( zeBook.author ) // print anotherAuthor
// of course if the first println didn't occur, we don't want now to fetch
the author in db with anotherAuthor

// and save it
// repositoryFactory.bookRepository.save( zeBook )





Ricky Clarkson wrote:
>
> Lazy Var Puzzler #1
>
> lazy var x = { println("Do you see me?")
>                5 }
> x = { println("Or me?")
>       10 }
>
> What gets printed?  Is this useful?
>
> 2008/11/6 mustaghattack <[hidden email]>:
>>
>> Just wondering why lazy can only apply to val and not var ?
>>
>> Thanks
>> --
>> View this message in context:
> http://www.nabble.com/lazy-var-tp20365847p20365847.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>

--
View this message in context: http://www.nabble.com/lazy-var-tp20365847p20379966.html
Sent from the Scala - User mailing list archive at Nabble.com.


dpp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lazy var

dpp


On Fri, Nov 7, 2008 at 7:09 AM, James Iry <[hidden email]> wrote:
It's not part of the language so you write your own and decide if it's too confusing to be useful.

  class LazyVar[T](init : => T) {
    private var thunk : (() => T) = {() => {thunk = {() => init}; thunk()}}
   
    def ! = synchronized { thunk() }
    def :=(newVal : => T) = synchronized { thunk = {() => {thunk = {() => newVal}; thunk()}}}
   
    def morph(f : T => T) = synchronized { val oldThunk = thunk ; thunk = { () => { thunk = {() => f(oldThunk())}; thunk()}}}                        
  }
  object LazyVar {
    def apply[T](init : => T) = new LazyVar({init})
  }

scala> val x = LazyVar(1)
x: LazyVar[Int] = LazyVar@5354a

scala> x := {println("assigning 2"); 2}

scala> x := {println("assigning 3"); 3}

scala> x!
assigning 3
res16: Int = 3

This is what we do in Lift.  It's helpful for setting defaults on fields in a record.  However, I see this as a library feature, not a language feature.
 





On Fri, Nov 7, 2008 at 4:48 AM, mustaghattack <[hidden email]> wrote:

What about this :

1- Simple domain

abstract class Person( var id : String ) {
 var name : String
}

abstract class Book( var isbn : String ) {
 var author : Person
}

2- Simple repositories

trait PersonRepository {
 def findById( id : String ) : Person
}

trait BookRepository {
 def findByIsbn( isbn : String ) : Book
}

trait BookService {
 def findByIsbn( isbn : String ) : xml.Node

class XmlBookRepository( personRepository : PersonRepository, bookService :
BookService ) {
 def findByIsbn( isbn : String ) : Book = {
    val bookNode = bookService.findByIsbn( isbn )
    new Book( ( bookNode attribute "isbn" get ).text )  {
        // load lazily the author
        override lazy var author = personRepository.findById( ( bookNode \
"author" ).text )
    }
 }
}

// plus factories and their implementation
trait RepositoryFactory( serviceFactory : ServiceFactory ) ...

3- Now you can fetch entities, and update them

// get a book
val repositoryFactory = new XmlRepositoryFactory( ... )
val zeBook = repositoryFactory.bookRepository.findByIsbn(
"123-0-123-12333-2" )
println( zeBook.author.name ) // fetch lazily the author, but you don't have
to ...

// update author
val anotherAuthor = repository.personRepository.findById( "234" )
zeBook.author = anotherAuthor
println( zeBook.author ) // print anotherAuthor
// of course if the first println didn't occur, we don't want now to fetch
the author in db with anotherAuthor

// and save it
// repositoryFactory.bookRepository.save( zeBook )





Ricky Clarkson wrote:
>
> Lazy Var Puzzler #1
>
> lazy var x = { println("Do you see me?")
>                5 }
> x = { println("Or me?")
>       10 }
>
> What gets printed?  Is this useful?
>
> 2008/11/6 mustaghattack <[hidden email]>:
>>
>> Just wondering why lazy can only apply to val and not var ?
>>
>> Thanks
>> --
>> View this message in context:
> http://www.nabble.com/lazy-var-tp20365847p20365847.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>

--
View this message in context: http://www.nabble.com/lazy-var-tp20365847p20379966.html
Sent from the Scala - User mailing list archive at Nabble.com.





--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
Loading...