Is there a way to set automatic properties in secondary constructors? - f#

I have this class:
type Sample() =
member val test1 = "" with get,set
member val test2 = "" with get,set
// is something like the below constructor possible
new Sample(result1, result2) =
this.test1 <- "failed"
this.test2 <- "passed"
Sample()
I've tried several different ways, but I can not get it to work.

Is this what you want?
type Sample(result1, result2) =
member val Test1 = result1 with get,set
member val Test2 = result2 with get,set
new () = Sample("failed", "passed")
FSI:
> Sample();;
val it : Sample = FSI_0002+Sample {Test1 = "failed";
Test2 = "passed";}
> Sample("foo", "bar");;
val it : Sample = FSI_0002+Sample {Test1 = "foo";
Test2 = "bar";}

#Mark Seemann's answer is the proper solution, but you can get exactly what you want using this odd construction:
type Sample() =
member val test1 = "" with get,set
member val test2 = "" with get,set
new (result1, result2) as sample =
Sample()
then
sample.test1 <- result1
sample.test2 <- result2
But truth be told, this is something I have never used myself, likely never seen used in the wild and more of a language-trivia sort of thing than anything else.

Related

Make WebSharper generate simple field access

I need a type that will be translated into a plain JS object so that F# field access will be translated into simple JS field access (this is necessary, since the object will be sent through postMessage, so it'll lose all its methods).
Again, I need
let x = a.b
to be translated into
var x = a.b;
without any method calls.
Here is a slightly modified example from the F# Reference:
namespace T
open IntelliFactory.WebSharper
[<JavaScript>]
module A =
type MyClass =
val a : int
val b : int
new(a0, b0) = { a = a0; b = b0; }
let myClassObj = new MyClass(35, 22)
let x = myClassObj.b
This won't translate with
x: error : Failed to translate property access: b.'
Ok, let's make those vals mutable:
namespace T
open IntelliFactory.WebSharper
[<JavaScript>]
module A =
type MyClass =
val mutable a : int
val mutable b : int
new(a0, b0) = { a = a0; b = b0; }
let myClassObj = new MyClass(35, 22)
let x = myClassObj.b
This will be successfully translated, but… MyClass.New returns an empty object. The question now starts looking much like a bugreport, right? So, back to the question.
Are there any other ways to achieve what I want?
There are additional issues with record-style constructors "{x = y}". I will have to look into this again on F# 3.0, the older F# did not produce sensible quotations for those and we did some partial workarounds in WebSharper. Right now your example breaks. So here is the working code with a static method instead of a constructor:
type MyClass private () =
[<DefaultValue>]
val mutable a : int
[<DefaultValue>]
val mutable b : int
static member Create(a0, b0) =
let c = MyClass()
c.a <- a0
c.b <- b0
c
let test () =
let myClassObj = MyClass.Create(35, 22)
let x = myClassObj.a
let y = myClassObj.b
JavaScript.Log(x, y)
Trivially, a record would also work.
In some cases where you want to go really low-level you can annotate members with the Inline attribute. When this is too much overhead you can use untyped API:
let x = obj ()
x?a <- 1
let y = x?a
JavaScript.Log(x, y)
try this:
type MyClass (a, b) =
member val A = a with get
member val B = b with get
let myClassObj = new MyClass(35, 22)
let x = myClassObj.B

how do i fix these errors generated by my computational expression that is using my custom workflow builder?

From the MSDN documentation I understand that if Run is implemented it will be called automatically at the end of the computational expression. It says that:
builder.Run(builder.Delay(fun () -> {| cexpr |}))
will be generated for the computational expression. Run and/or Delay will be omitted if they are not defined in the workflow builder. I was expecting my ReaderBuilder to return a list of MyItem objects when Run is called automatically. So I do not understand why I'm getting a type mismatch error. The errors are generated by the return statement inside the ProcedureBuilder foo at the end of my code listing here. Could someone please explain what I'm misunderstanding about workflow builders and what I have implemented incorrectly?
I'm getting the following errors:
The type ''a list' is not compatible with the type 'ReaderBuilder'
Type constraint mismatch. The type 'a list is not compatible with type ReaderBuilder The type ''a list' is not compatible with the type 'ReaderBuilder'
open System
open System.Data
open System.Data.Common
open System.Configuration
let config = ConfigurationManager.ConnectionStrings.Item("db")
let factory = DbProviderFactories.GetFactory(config.ProviderName)
type Direction =
| In
| Out
| Ref
| Return
type dbType =
| Int32
| String of int
type ReaderBuilder(cmd) =
let mutable items = []
member x.Foo = 2
member x.YieldFrom item =
items <- item::items
item
member x.Run item =
items
type ProcBuilder(procedureName:string) =
let name = procedureName
let mutable parameters = []
let mutable cmd:DbCommand = null
let mutable data = []
member x.Command with get() = cmd
member x.CreateCommand() =
factory.CreateCommand()
member x.AddParameter(p:string*dbType*Direction) =
parameters <- p::parameters
member x.Bind(v,f) =
f v
member x.Reader = ReaderBuilder(cmd)
member x.Return(rBuilder:ReaderBuilder) =
data
let (?<-) (builder:ProcBuilder) (prop:string) (value:'t) =
builder.Command.Parameters.[prop].Value <- value
type MyItem() =
let mutable _a = 0
let mutable _b = String.Empty
let mutable _c = DateTime.Now
member x.a
with get() = _a
and set n = _a <- n
member x.b
with get() = _b
and set n = _b <- n
member x.c
with get() = _c
and set n = _c <- n
let proc name = ProcBuilder(name)
let (%) (builder:ProcBuilder) (p:string*dbType*Direction) =
builder.AddParameter(p)
builder
let (?) (r:DbDataReader) (s:string) = r.GetOrdinal(s)
let foo x y =
let foo = proc "foo" % ("x", Int32, In) % ("y", String(15), In)
foo?x <- x
foo?y <- y
foo {
do! foo?x <- x
do! foo?y <- y
return foo.Reader {
let item = MyItem()
item.a <- r.GetInt32("a")
item.b <- r.GetString("b")
item.c <- r.GetDateTime("c")
yield! item
}
}
The problem in your example is that the foo.Reader { ... } block has a return type MyItem list (because this is what the Run member of the ReaderBuilder type returns). However, the Return member of ProcBuilder expects an argument of type ReaderBuilder.
The data field of ReaderBuilder will be always an empty list, so this is also suspicious. I think you probably want to change the Return of ProcBuilder to take an argument MyItem list instead.
However, I think that using custom computation builder for database access doesn't really give you much advantage. You're not creating a "non-standard computation" in some sense. Instead, you probably just want a nice syntax for calling commands & reading data. Using the dynamic operator can make this quite elegant even without computation builders - I wrote an article about this some time ago.

F#: Can I use try/with in a constructor that calls to the base class?

I'm a newbie F# programmer, and I'm having some trouble getting the syntax for my F# program correct.
Essentially, I want to turn this C# code into F#:
class MyRiskyObject : BaseObject
{
private string field;
public MyRiskyObject(object foo, string data)
: base(foo)
{
try
{
this.data = RiskyOperation(data);
}
catch (ArgumentException)
{
DoSomethingElse();
}
}
}
I, so far, have something like
type MyRiskyObject
inherit BaseObject
val field : string
new (foo:object, data:string) = {
inherit BaseObject(foo)
try
field = RiskyOperation()
????????
}
I just can't get the syntax right...
Edit:
here is the actual code I'm working on:
type RegExpObject =
inherit CommonObject
val RegExp : Regex
val Global : bool
member x.IgnoreCase:bool =
(x.RegExp.Options &&& RegexOptions.IgnoreCase) = RegexOptions.IgnoreCase
member x.MultiLine:bool =
(x.RegExp.Options &&& RegexOptions.Multiline) = RegexOptions.Multiline
new (env, pattern, options, global') =
{
inherit CommonObject(env, env.Maps.RegExp, env.Prototypes.RegExp)
// Here, I need to catch the exception, and instead call RaiseSyntaxError.
RegExp = new Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)
Global = global'
}
then RegExp = new Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)
new (env, pattern) =
RegExpObject(env, pattern, RegexOptions.None, false)
Why not just delegate the real work to a separate free function, so the work doesn't have to be done directly in your class' constructor?
let createRegex pattern options =
try
Regex(pattern, options ||| RegexOptions.ECMAScript ||| RegexOptions.Compiled)
with
| :? System.ArgumentException -> RaiseSynaxError ()
Then your class (as demonstrated in your edit) would be:
type RegExpObject(env, pattern, options, global') //'
inherit CommonObject(env, env.Maps.RegExp, env.Prototypes.RegExp)
let RegExp = createRegex pattern options
let Global = global' //'
member x.IgnoreCase =
(x.RegExp.Options &&& RegexOptions.IgnoreCase) = RegexOptions.IgnoreCase
member x.MultiLine =
(x.RegExp.Options &&& RegexOptions.Multiline) = RegexOptions.Multiline
new (env, pattern) = RegExpObject(env, pattern, RegexOptions.None, false)
An advantage here is that, as #Brian indicated, no use of val would be necessary, making the class definition much cleaner.
Try the following
type MyRiskyObject(foo : obj, data : string) as this =
inherit BaseObject(foo)
let mutable data = data;
do
try
data <- this.RiskyOperation data
with
| :? System.ArgumentException -> this.DoSomethingElse()
Alternate example with non-mutable let binding where RiskOperation and DoSomethingElse are not members of MyRiskObject
type MyRiskyObject(foo : obj, data : string) =
inherit BaseObject(foo)
let data =
try
OtherModule.RiskyOperation data
with
| :? System.ArgumentException ->
OtherModule.DoSomethingElse()
data

member list ref not updating

After a hiatus from my Silverlight / F# application, i am starting back into it and am running into an issue I cannot seem to get my head around. I have a member variable of my usercontrol that is a list ref, on button clicks I want to add records to it - but it never updates. I am pretty sure it has to do with being a member but I havent figured it out.
Thanks in advance to those who take the time to view and reply.
the problem lines:
.
.
.
member this.brokers = ref List.empty
.
.
.
// this line doesn't seem to work
this.brokers := candidate :: (!this.brokers)
.
.
.
the class:
type Page() as this =
inherit UriUserControl("/xyz;component/page.xaml", "page")
do
this.firm.ItemsSource <- RpcXYZ.getFirms()
this.email.Background <- SolidColorBrush(Colors.Red)
this.addBtn.IsEnabled <- false
()
// instance data
member this.brokers = ref List.empty
// bound controls for add candidate
member this.FE : FrameworkElement = (this.Content :?> FrameworkElement)
member this.fname : TextBox = this.FE ? fname
member this.lname : TextBox = this.FE ? lname
member this.email : TextBox = this.FE ? email
member this.firm : RadComboBox = this.FE ? firms
member this.addBtn : RadButton = this.FE ? addBtn
member this.addCadidate_Click (sender : obj) (args : RoutedEventArgs) =
let inline findFirm (f : RpcXYZ.firm) =
f.id = Int32.Parse(this.firm.SelectedValue.ToString())
let candidate : SalesRep = {
id = -1 ;
fname = this.fname.Text ;
lname = this.lname.Text ;
email = this.email.Text ;
phone = "" ;
firm = List.find findFirm <| RpcXYZ.getFirms();
score = None ;
}
// this line is fine t is a list of 1 item after 1 click
let t = candidate :: (!this.brokers)
// this line doesn't seem to work
this.brokers := candidate :: (!this.brokers)
ChildWindow().Show() |> ignore ;
member this.email_Changed (o : obj) (arg : TextChangedEventArgs) =
let txtBox = (o :?> TextBox)
let emailRegex = Regex("(\w[-._\w]*\w#\w[-._\w]*\w\.\w{2,3})")
if emailRegex.IsMatch(txtBox.Text) = false then
txtBox.Background <- SolidColorBrush(Colors.Red)
this.addBtn.IsEnabled <- false
else
txtBox.Background <- new SolidColorBrush(Colors.White)
this.addBtn.IsEnabled <- true
This
member this.brokers = ref List.Empty
defines a property getter. Every time you touch .brokers, it re-runs the code on the right hand side. That's the issue.
The fix would be to define an instance variable, and return that:
let brokers = ref List.Empty
member this.Brokers = brokers
Then a single ref is allocated when an instance of the class is constructed, and you keep accessing that same ref object via the member property.
Brian already explained the problem. However, is there any reason why you're not using a mutable member (with getter and setter) and instead use a readonly member that returns a reference cell?
Using a get/set member would be more idiomatic solution:
let mutable brokers = List.Empty
member this.Brokers
with get() = brokers
and set(value) = brokers <- value
The declaration is a bit longer (unfortunately, there are no automatic properties in F#!), but the member will look like a standard property (from both F# and C#). You could then use it like this:
x.Brokers <- candidate :: x.Brokers
Although, you need the property only for public members that should be accessed from outside of the type. For private fields, you can just use the mutable value brokers directly...

F# record member evaluation

Why is t.b evaluated on every call? And is there any way how to make it evaluate only once?
type test =
{ a: float }
member x.b =
printfn "oh no"
x.a * 2.
let t = { a = 1. }
t.b
t.b
An alternative version of Brian's answer that will evaluate b at most once, but won't evaluate it at all if B is never used
type Test(a:float) =
// constructor
let b = lazy
printfn "oh no"
a * 2.
// properties
member this.A = a
member this.B = b.Value
It's a property; you're basically calling the get_b() member.
If you want the effect to happen once with the constructor, you could use a class:
type Test(a:float) =
// constructor
let b = // compute it once, store it in a field in the class
printfn "oh no"
a * 2.
// properties
member this.A = a
member this.B = b
In response to your comments in Brian's post, you can fake copy-and-update record expressions using optional/named args. For example:
type Person(?person:Person, ?name, ?age) =
let getExplicitOrCopiedArg arg argName copy =
match arg, person with
| Some(value), _ -> value
| None, Some(p) -> copy(p)
| None, None -> nullArg argName
let name = getExplicitOrCopiedArg name "name" (fun p -> p.Name)
let age = getExplicitOrCopiedArg age "age" (fun p -> p.Age)
member x.Name = name
member x.Age = age
let bill = new Person(name = "Bill", age = 20)
let olderBill = new Person(bill, age = 25)
printfn "Name: %s, Age: %d" bill.Name bill.Age
printfn "Name: %s, Age: %d" olderBill.Name olderBill.Age
The previous responses suggest switching to a class, instead of using a record. If you want to stay with records (for their simple syntax and immutability), you can take this approach:
type test =
{ a : float
b : float }
static member initialize (t: test) =
{ t with b = t.a * 2. }
This is useful if the instance of test is created by another library (like a data provider from a web service or database). With this approach, you must remember to pass any instance of test that you receive from that API through the initialize function before using it in your code.

Resources