Let's say there's a class that with one public constructor, which takes one parameter. In addition, there are also multiple public properties I'd like to set. What would be the syntax for that in F#? For instance in C#
public class SomeClass
{
public string SomeProperty { get; set; }
public SomeClass(string s)
{ }
}
//And associated usage.
var sc = new SomeClass("") { SomeProperty = "" };
In F# I can get this done using either the constructor or property setters, but not both at the same time as in C#. For example, the following aren't valid
let sc1 = new SomeClass("", SomeProperty = "")
let sc2 = new SomeClass(s = "", SomeProperty = "")
let sc3 = new SomeClass("")(SomeProperty = "")
It looks like I'm missing something, but what?
<edit: As pointed out by David, doing it all in F# works, but for some reason, at least for me :), it gets difficult when the class to be used in F# is defined in C#. As for an example on such is TopicDescription (to make up something public enough as for an added example). One can write, for instance
let t = new TopicDescription("", IsReadOnly = true)
and the corresponding compiler error will be Method 'set_IsReadOnly' is not accessible from this code location
.
Definition: The Fahrenheit (symbol: °F) is a unit of temperature that was widely used prior to metrication.
Definition of in (Entry 2 of 11) 1 a (1) : to or toward the inside especially of a house or other building come in (2) : to or toward some destination or particular place flew in on the first plane (3) : at close quarters : near play close in
11-letter words that end in f. bulletproof. tamperproof. neckerchief. bullmastiff. mantelshelf. leatherleaf.
A temperature interval of 1 °F is equal to an interval of 5⁄9 degrees Celsius. The Fahrenheit and Celsius scales intersect at −40° (i.e. −40 °F = −40 °C).
The problem you're having is that IsReadOnly
has an internal setter.
member IsReadOnly : bool with get, internal set
If you want to set it directly you're going to need to subclass TopicDescription
.
The constructor syntax you're looking at is perfectly acceptable.
let test = new Microsoft.ServiceBus.Messaging.TopicDescription("", EnableBatchedOperations=true)
compiles just fine for me.
I never program in F#, but this seems to work fine for me:
type SomeClass(s : string) =
let mutable _someProperty = ""
let mutable _otherProperty = s
member this.SomeProperty with get() = _someProperty and set(value) = _someProperty <- value
member this.OtherProperty with get() = _otherProperty and set(value) = _otherProperty <- value
let s = new SomeClass("asdf", SomeProperty = "test");
printf "%s and %s" s.OtherProperty s.SomeProperty;
That outputs "asdf and test"
.
Additionally, the following code works fine for me:
public class SomeClass
{
public string SomeProperty { get; set; }
public string OtherProperty { get; set; }
public SomeClass(string s)
{
this.OtherProperty = s;
}
}
Then in F#:
let s = SomeClass("asdf", SomeProperty = "test")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With