简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM