Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# static member

Tags:

f#

I was working through the PDC 2008 F# videos and seem to run into a problem

type StockAnalyzer (lprices, days) =
let prices =
    lprices
    |> Seq.map snd
    |> Seq.take days
 static member GetAnalyzers(tickers, days) = 
    tickers
    |> Seq.map loadPrices
    |> Seq.map (fun prices -> new StockAnalyzer(prices, days))

 member s.Return = 
    let lastPrice = prices |> Seq.nth 0
    let startPrice = prices |> Seq.nth (days - 1)
    lastPrice / startPrice - 1.

I am getting an error at the static.

GetStockPrices.fs(31,6): error FS0010: Unexpected keyword 'static' in binding. Expected incomplete structured construct at or before this point or other token.
Does anyone know if they have changed the syntax or can spot what I am doing wrong
like image 453
HeMan Avatar asked Mar 07 '10 23:03

HeMan


2 Answers

F# uses significant white space. Add a space in front of "let prices =". The compiler is trying to figure out why you have a static member of "prices", because the only preceding line with less indentation is "let prices =".

You may want to use more indentation, just for clarity.

type StockAnalyzer (lprices, days) =
   let prices =
      lprices
      |> Seq.map snd
      |> Seq.take days

   static member GetAnalyzers(tickers, days) = 
      tickers
      |> Seq.map loadPrices
      |> Seq.map (fun prices -> new StockAnalyzer(prices, days))

   member s.Return = 
      let lastPrice = prices |> Seq.nth 0
      let startPrice = prices |> Seq.nth (days - 1)
      lastPrice / startPrice - 1.
like image 100
David Morton Avatar answered Nov 15 '22 09:11

David Morton


The indentation before the word static is confusing the compiler and it's trying to interpret it as part of the let expression. The let expression should be indented and the member definitions should be in line with it.

like image 39
Chuck Avatar answered Nov 15 '22 10:11

Chuck