The lazy implementation of "Force" does slightly more that your hand coded version, for example it checks for exceptions and if an exception is raised it will reproduce it each time the property is called. I would therefore expect it to be slightly slower, however I wouldn't expect this overhead to be a problem in most situations.

The source code for lazy values is available so it should be fairly staightforward to look at the extra work its doing and decide if this overhead is worth it.

Cheers,
Rob

By on 3/3/2008 12:33 AM ()

My own attempts an implementing lazy properties led me to these posts. It seems to me that the original poster's first idea does indeed implement a lazy property, albeit by hand, whereas the second attempt to using the lazy keyword actually creates a new Lazy instance each time it is called, which is then instantly forced! This would explain the slowness of the 'cleaner' solution.

I had independently arrived at something similar to the second solution, only to run into the same slowness. Replacing with the first by-hand solution knocked 86% off the running time of my program, which saves me about 7 minutes per run!

Is there an elegant way to implement a lazy property using the lazy keyword?

Rob Smallshire

By on 10/16/2008 2:30 PM ()

I have not tried it, but I think just replace "member private"s with "let"s. The private member is a property which gets re-evaluated each time, whereas the let is more like a "field" that is only evaluated once at construction time.

By on 10/16/2008 3:21 PM ()

Hi,

I want lazy evaluation in an augmentation. Is this possible?

1
2
3
4
5
6
7
8
9
10
11
12
type Velocity =  
  {V:float<m/s>}
  
  static member FromMPS(v: float<m/s>) =
    {new Velocity with V = v}
    
  static member FromKPH(v: float<km/h>) =
    {new Velocity with V = v * km.per_meter / h.per_sec}
    
  member x.MPS = x.V     
  member x.KPH = x.V / km.per_meter * h.per_sec
  override x.ToString() =  sprintf "%A kph" x.KPH

When I try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
type Velocity =  


  {V:float<m/s>}


  


  static member FromMPS(v: float<m/s>) =


    {new Velocity with V = v}


    


  static member FromKPH(v: float<km/h>) =


    {new Velocity with V = v * km.per_meter / h.per_sec}


    


  let MPS = x.V     


  let KPH = x.V / km.per_meter * h.per_sec


  override x.ToString() =  sprintf "%A kph" x.KPH

I get "This declaration element is not permitted in an augmentation".

Best regards,
Steffen
<m s=""><m s=""><km h=""><m s=""><m s=""><km h=""></km></m></m></km></m></m>

By on 1/27/2009 1:31 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper