Best way to determine variable type and treat each one differently in F#
Posted
by James Black
on Stack Overflow
See other posts from Stack Overflow
or by James Black
Published on 2010-05-12T00:36:48Z
Indexed on
2010/05/12
0:44 UTC
Read the original article
Hit count: 267
I have a function that will create a select where clause, but right now everything has to be a string.
I would like to look at the variable passed in and determine what type it is and then treat it properly.
For example, numeric values don't have single quotes around them, option type will either be null or have some value and boolean will actually be zero or one.
member self.BuildSelectWhereQuery (oldUser:'a) =
let properties = List.zip oldUser.ToSqlValuesList sqlColumnList
let init = false, new StringBuilder()
let anyChange, (formatted:StringBuilder) =
properties |> Seq.fold (fun (anyChange, sb) (oldVal, name) ->
match(anyChange) with
| true -> true, sb.AppendFormat(" AND {0} = '{1}'", name, oldVal)
| _ -> true, sb.AppendFormat("{0} = '{1}'", name, oldVal)
) init
formatted.ToString()
Here is one entity:
type CityType() =
inherit BaseType()
let mutable name = ""
let mutable stateId = 0
member this.Name with get() = name and set restnameval=name <- restnameval
member this.StateId with get() = stateId and set stateidval=stateId <- stateidval
override this.ToSqlValuesList = [this.Name; this.StateId.ToString()]
So, if name
was some other value besides a string, or stateId
can be optional, then I have two changes to make:
- How do I modify ToSqlValuesList to have the variable so I can tell the variable type?
- How do I change my select function to handle this?
I am thinking that I need a new function does the processing, but what is the best FP way to do this, rather than using something like typeof
?
© Stack Overflow or respective owner