1

I am getting an error when trying to use the GetSlice() method.

cl.Rows.GetSlice( DateTime(2014,12,28,20,0,0), DateTime(2014,12,28,23,0,0))

The error is: This expression was expected to have type DateTime option but here has type DateTime. This error is given for both parameters ( DateTime(2014,12,28,20,0,0), DateTime(2014,12,28,23,0,0) ) in the above code

Could some one explain the differnce between type of 'DateTime option' a type of 'DateTime'. What would be the correct correct syntax to use for the above code?

FsLab version(0.3.17)

rhscJohn
  • 99
  • 7
  • 1
    See: [Options (F#)](https://msdn.microsoft.com/en-us/library/dd233245.aspx) and [The Option type](https://fsharpforfunandprofit.com/posts/the-option-type/) – Guy Coder Mar 24 '16 at 18:56

2 Answers2

1

The method GetSlice expects both parameter of type DateTime option (as the compiler helpfully tells you), but you're providing just DateTime.

To create a value of type 't option, use constructor Some:

cl.Rows.GetSlice( Some (DateTime(2014,12,28,20,0,0)), Some (DateTime(2014,12,28,23,0,0)) )
Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
0

Option is a type which either encapsulates a value of a certain type or it indicates that there's no value. In F#, there are two ways to write its type: Option<'t> or 't option.

The main motivation behind using option is to produce type-save and bug-free code - it makes using nulls and some Exceptions (e.g. KeyNotFoundExcpetions) obsolete.

Tomasz Maczyński
  • 973
  • 10
  • 24
  • I am very new to F# and had come across the Type Option. But I was unable to make the connection when I saw 'DateTime option'. The biggest problem I am having with deedle is understanding the syntax of what the compiler tells me it is expecting. Until I am able to comprehend the syntax I am left to guessing as to what to enter. – rhscJohn Mar 25 '16 at 18:02
  • If you find yourself unable to comprehend the syntax, that may indicate that some initial learning of concepts is in order. I recommend starting with the excellent resource http://fsharpforfunandprofit.com, and specifically for your case, [this section on expressions and syntax](http://fsharpforfunandprofit.com/series/expressions-and-syntax.html). – Fyodor Soikin Mar 25 '16 at 23:14
  • @rhscJohn - I thought that you were confused because you didn't understand what "'t option" means (it might be something new for somebody from C# or Java background) and that you are familiar with basics of F#'s typesystem and documentation. I agree with Fyodor - fsharpforfunandprofit.com is a great learning resource and I also recommend it. – Tomasz Maczyński Mar 26 '16 at 16:31