开发者

F# static member

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜