Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Option to value with default value

Tags:

f#

option-type

With the help of answer of this question I need help with the specific syntax on how to retrieve values from option types in the following case.

type Query = {
    q : string
    pageSize : int option
}

let search (query : Query) =
    let url = sprintf "foo.com?q=%spageSize=%i" query.q (query.pageSize |> 10 |< query.pageSize) // ???

Syntax help for (query.pageSize |> 10 |< query.pageSize)

like image 955
Developer11 Avatar asked Mar 16 '18 13:03

Developer11


People also ask

How do I set default value in options?

The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

What is defaultValue?

The DefaultValue property specifies text or an expression that's automatically entered in a control or field when a new record is created. For example, if you set the DefaultValue property for a text box control to =Now(), the control displays the current date and time.

How do you set the default value of a select option when mapping over an array?

So, you can set a default value by passing the value of the option in the value prop of the select input element. This is very convenient in a controlled component as you only have to update the value in one place.


2 Answers

Option.defaultValue is your friend:

type Query = {
  q : string
  pageSize : int option
}
let q = {q = "foo"; pageSize = None}
let p = q.pageSize |> Option.defaultValue 10
like image 63
Taylor Wood Avatar answered Sep 21 '22 21:09

Taylor Wood


The answer you linked provides a pretty clear illustration of the syntax:

input |> defaultArg <| ""

In your case, the input is query.pageSize and the default value is 10 instead of empty string. So:

query.pageSize |> defaultArg <| 10
like image 31
Fyodor Soikin Avatar answered Sep 18 '22 21:09

Fyodor Soikin