Can F# use a constructor parameter and property initializers together? - f#

In C# I have the following code to create an object instance.
var myObject = new MyClass("paramvalue")
{
Property1 = "value1"
Property2 = 100
}
I've found the F# syntax to set the properties in the initializer like:
let myObject = MyClass(Property1 = "value1", Property2 = 100)
Is there also an option to supply both constructor parameter(s) and property values together?

This should work:
let myObject = MyClass("paramvalue", Property1 = "value1", Property2 = 100)
See the documentation for another example.

Related

Iterate over static properties of a struct

Is there a simple way to iterate over all of the properties of a struct? The approach for non-static properties that I am familiar with is using Mirror, but this returns an empty array when the struct contains only static properties. Here's an example of what I am trying to achieve:
struct Tree {
static let bark = "Bark"
static let roots = "Roots"
}
let treeParts = [String]()
// insert code here to make treeParts = ["Bark", "Roots"]
Since I also have an interest of how to do this I made the example below. Why not just create the struct with non static properties plus a static instance variable which makes the struct a singleton. The following code sample details an example use case for mapping values of a Person object to a JSON dictionary using the REST API's naming semantics. The property names of PersonJSONKeys have to match the property names of Person.
The code for the allProperties() function comes from How to loop over struct properties in Swift?. You can modify this function easily to only return the values of a structs properties.
struct PersonJSONKeys: PropertyLoopable {
static let instance: PersonJSONKeys = PersonJSONKeys()
private init() {}
let name = "name"
let firstName = "first_name"
let age = "age"
}
struct Person: PropertyLoopable {
let name = "Doe"
let firstName = "John"
let age = "18"
}
let person = Person()
let personProperties = person.allProperties()
var personJSON: [String:Any] = [:]
for jsonProperty in PersonJSONKeys.instance.allProperties() {
let propertyName = jsonProperty.key
let jsonKey = jsonProperty.value as! String
personJSON[jsonKey] = personProperties[propertyName]
}
Since the Struct is now a singleton all of its properties will be initialised only once and the thread safety is given by its static instance variable.
I was also looking for this and ended up using an enum. It's a good thing if you only have a list of static values (but enums also come with some limitations, e.g. no extensions...):
enum Tree: String, CaseIterable {
case bark = "Bark"
case roots = "Roots"
}
let allTrees = Tree.allCases.map{ $0.rawValue }
print(allTrees) // => ["Bark", "Roots"]

Print `po` object description with println()?

If I'm to do this:
class MyObject {
var name = "Something"
}
var myObject = MyObject()
And then in the console at runtime, po myObject, I get this:
0x00007fe1596ef890
(name = "Something")
In other words, it prints out the property values of every property in the object.
I want to replicate this behaviour using println, but doing println(myObject) just gives me:
MyApp.MyObject
How can I get it to print out the property values, the same it would if I were to use the po command.
If you make your class conform to the Printable protocol, a description computed property will be printed instead of the type name:
class MyObject: Printable {
var name = "Something"
var description: String {
return "MyObject(name = \(self.name))"
}
}
let myObject = MyObject()
println(myObject)
// MyObject(name = Something)
Alternately, you can use dump() instead of println() to dump an object's properties using Swift's reflection tools:
dump(myObject)
// ▿ __lldb_expr_920.MyObject #0
// - name: Something

F# alternate constructor assigning values to (mutable) let bindings

Suppose I have this class:
type Pet (name:string) as this =
let mutable age = 5
let mutable animal = "dog"
I want to be able to create a new Pet based on some serialized data, which I represent with this record:
type PetData = {
name : string
age : int
animal : string
}
(TLDR: I can't figure out the syntax to make a constructor that'll take a PetData to populate the let bindings. My various attempts follow.)
So I make a new Pet constructor that'll assign values to the let bindings. I try using the class initializer syntax:
new (data:PetData) =
Pet(name,
age = data.age,
animal = data.animal
)
Hmm, nope: No accessible member or object constructor named 'Pet' takes 1 arguments. The named argument 'age' doesn't correspond to any argument or settable return property for any overload.
I check to make sure I've got all the syntax: no missing commas, correct "assignment" (cough) operator, correct indentation.
Okay the, I'll try the record initializer syntax.
new (data:PetData) =
{
name = data.name;
age = data.age;
animal = data.name
}
Error: The type 'Pet' does not contain a field 'name'
Okay, so I need to call the main constructor. I guess there are probably two places I can put it, so let's try both:
new (data:PetData) =
{
Pet(data.name);
age = data.age;
animal = data.name
}
Nope: Invalid object, sequence or record expression
new (data:PetData) =
Pet(data.name)
{
age = data.age;
animal = data.name
}
And nope: This is not a valid object construction expression. Explicit object constructors must either call an alternate constructor or initialize all fields of the object and specify a call to a super class constructor.
I didn't want to have to do this, but maybe since the fields are mutable anyway, I can just assign values to the object after initializing it:
new (data:PetData) =
let p = Pet(data.name)
p.age <- data.age
p.animal <- data.animal
p
Type constraint mismatch. The type Pet is not compatible with type PetData The type 'Pet' is not compatible with the type 'PetData'
Lol, what??
Okay, let's try this:
let assign(data:PetData) =
this.age <- data.age
this.animal <- data.animal
new (data:PetData) =
let p = Pet(data.name)
p.assign(data)
p
The field, constructor or member 'assign' is not defined
Right, so it can't access let bindings from outside.
Let's try a member then:
new (data:PetData) =
let p = Pet(data.name)
p.Assign(data)
p
member x.Assign(data:PetData) =
this.age <- data.age
this.animal <- data.animal
This is not a valid object construction expression. Explicit object constructors must either call an alternate constructor or initialize all fields of the object and specify a call to a super class constructor.
Okay... let's try this whole thing differently then, using explicit fields:
type Pet =
[<DefaultValue>]val mutable private age : int
[<DefaultValue>]val mutable private animal : string
val private name : string
new(name:string) =
{ name = name }
new(data:PetData) =
{
name = data.name;
age = data.age;
animal = data.animal
}
Extraneous fields have been given values
And that's when I punch my elderly cat in the face.
Any other ideas? These error messages are throwing me off. I can't even find half of them on Google.
You could do this.
type Pet =
val mutable private age : int
val mutable private animal : string
val private name : string
new (name:string) =
{
name = name;
age = 5; // or age = Unchecked.defaultof<_>;
animal = "dog"; // or animal = Unchecked.defaultof<_>;
}
new (data:PetData) =
{
name = data.name;
age = data.age;
animal = data.animal;
}
F# has its own style which looks like this.
type Pet(name:string, age:int, animal:string) =
let mutable age = age
let mutable animal = animal
new (name:string) =
Pet(name, 5, "dog")
new (data:PetData) =
Pet(data.name, data.age, data.animal)
Edit
Added an event used in do per comment request.
type Pet(name:string, age:int, animal:string, start:IEvent<string>) =
let mutable age = age
let mutable animal = animal
// all three constructors will call this code.
do start.Add (fun _ -> printf "Pet was started")
new (name:string, start:IEvent<_>) =
// an example of different logic per constructor
// this is called before the `do` code.
let e = start |> Event.map (fun x -> x + " from 'name constructor'")
Pet(name, 5, "dog", e)
new (data:PetData, start:IEvent<_>) =
Pet(data.name, data.age, data.animal, start)
Let bindings in a type are private and there's not much you could do about that. As such you cannot use Named Arguments. By creating properties you can do it like so, but not from inside the Pet type:
type Pet (name:string) =
let mutable age = 5
let mutable animal = "dog"
member x.Age with get () = age and set v = age <- v
member x.Animal with get () = animal and set v = animal <- v
type PetData = {
name : string
age : int
animal : string
}
with
member x.ToPet =
new Pet (x.name, Age = x.age, Animal = x.animal)
The other option would be to create a more general constructor like Gradbot suggested, either accepting a PetData object directly or all three parameters.

Overloading constructor without initialization

I'm writing a generic class that has two constructors: the first one initializes every field, the second (parameter-less) should not initialize anything.
The only way I found to achieve this is calling the main constructor with "empty" arguments, i.e. Guid.Empty and null. Besides not looking good functional style to my untrained eyes, this means that I have to put a a' : null constraint on the second parameter, which I don't want:
type Container<'a when 'a : null>(id : Guid, content : 'a) =
let mutable _id = id
let mutable _content = content
new() = Container<'a>(Guid.Empty, null)
member this.Id
with get() = _id
and set(value) = _id <- value
member this.Content
with get() = _content
and set(value) = _content <- value
I see two ways to solve this:
use something like the default c# keyword instead of null (does such a thing exist in F#?)
use a different syntax to specify constructors and private fields (how?)
What is the best way to implement this class?
The F# analog to default is Unchecked.default<_>. It is also possible to use explicit fields which you don't initialize:
type Container<'a>() =
[<DefaultValue>]
val mutable _id : Guid
[<DefaultValue>]
val mutable _content : 'a
new (id, content) as this =
new Container<'a>() then
this._id <- id
this._content <- content
However, in general, your overall approach is somewhat unidiomatic for F#. Typically you'd use a simple record type (perhaps with a static method to create uninitialized containers, although this seems to have questionable benefit):
type 'a Container = { mutable id : Guid; mutable content : 'a } with
static member CreateEmpty() = { id = Guid.Empty; content = Unchecked.defaultof<_> }
In many situations, you could even use an immutable record type, and then use record update statements to generate new records with updated values:
type 'a Container = { id : Guid; content : 'a }
[<GeneralizableValue>]
let emptyContainer<'a> : 'a Container =
{ id = Guid.Empty;
content = Unchecked.defaultof<_> }
let someOtherContainer = { emptyContainer with content = 12 }
If the type will be used from languages other than F#, the following provides a natural interface in F#, and C#, for example.
type Container<'a>(?id : Guid, ?content : 'a) =
let orDefault value = defaultArg value Unchecked.defaultof<_>
let mutable _id = id |> orDefault
let mutable _content = content |> orDefault
new() = Container(?id = None, ?content = None)
new(id : Guid, content : 'a) = Container<_>(?id = Some id, ?content = Some content)
member this.Id
with get() = _id
and set(value) = _id <- value
member this.Content
with get() = _content
and set(value) = _content <- value
If it will only be used from F#, you can omit the following constructor overloads
new(id : Guid, content : 'a) = Container<_>(?id = Some id, ?content = Some content)
new() = Container()
because the overload accepting optional args handles both these cases equally well in F#.

Using NoRM to access MongoDB from F#

Testing out NoRM https://github.com/atheken/NoRM from F# and trying to find a nice way to use it. Here is the basic C#:
class products
{
public ObjectId _id { get; set; }
public string name { get; set; }
}
using (var c = Mongo.Create("mongodb://127.0.0.1:27017/test"))
{
var col = c.GetCollection<products>();
var res = col.Find();
Console.WriteLine(res.Count().ToString());
}
This works OK but here is how I access it from F#:
type products() =
inherit System.Object()
let mutable id = new ObjectId()
let mutable _name = ""
member x._id with get() = id and set(v) = id <- v
member x.name with get() = _name and set(v) = _name <- v
Is there an easier way to create a class or type to pass to a generic method?
Here is how it is called:
use db = Mongo.Create("mongodb://127.0.0.1:27017/test")
let col = db.GetCollection<products>()
let count = col.Find() |> Seq.length
printfn "%d" count
Have you tried a record type?
type products = {
mutable _id : ObjectId
mutable name : string
}
I don't know if it works, but records are often good when you just need a class that is basically 'a set of fields'.
Just out of curiosity, you can try adding a parameter-less constructor to a record. This is definitely a hack - in fact, it is using a bug in the F# compiler - but it may work:
type Products =
{ mutable _id : ObjectId
mutable name : string }
// Horrible hack: Add member that looks like constructor
member x.``.ctor``() = ()
The member declaration adds a member with a special .NET name that is used for constructors, so .NET thinks it is a constructor. I'd be very careful about using this, but it may work in your scenario, because the member appears as a constructor via Reflection.
If this is the only way to get succinct type declaration that works with libraries like MongoDB, then it will hopefuly motivate the F# team to solve the problem in the future version of the language (e.g. I could easily imagine some special attribute that would force F# compiler to add parameterless constructor).
Here is a pretty light way to define a class close to your C# definition: it has a default constructor but uses public fields instead of getters and setters which might be a problem (I don't know).
type products =
val mutable _id: ObjectId
val mutable name: string
new() = {_id = ObjectId() ; name = ""}
or, if you can use default values for your fields (in this case, all null):
type products() =
[<DefaultValue>] val mutable _id: ObjectId
[<DefaultValue>] val mutable name: string

Resources