Is this what you want?

1
2
3
4
5
6
7
8
9
type Counter(anothercurrent) = class
    let mutable current = anothercurrent       
    new(firstcurrent, secondcurrent) =
        if firstcurrent > secondcurrent then
            Counter(firstcurrent + secondcurrent)
        else
            Counter(firstcurrent - secondcurrent)
    member obj.Current = current
end
By on 5/30/2010 1:07 PM ()

well, in this case yes.
But explicit constructor can contain more complex statements then that, statements that usually come after "then" keyword, in a class definition without an implicit constructor.

By on 5/31/2010 1:39 AM ()

I want to do something like this in F#:

class A
{
int i;
A(int i)
{
this.i = i;
Console.WriteLine("´Constructor(int)");
}
A(int i, int j) : this(i + j)
{
Console.WriteLine("´Constructor(int, int)");
}
}

By on 5/31/2010 1:48 AM ()
1
2
3
4
5
6
type A(x) =
    do printfn "Implicit constructor (int)"
    let i = x
    new(x1, y1) = 
        new A(x1 + y1)
        then printfn "Explicit constructor(int, int)"
By on 5/31/2010 4:04 AM ()

Exactly like I did above.
But as I described above, I gain a compilation error.

By on 5/31/2010 4:52 AM ()

I cannot access 'current' after "then" keyword.

By on 5/31/2010 4:55 AM ()

Desco's (and mine) work for me in F# 2.0.

By on 5/31/2010 5:04 AM ()

For me too.

But the bellow code doesn't work:

type Counter(anothercurrent) = class
let mutable current =

anothercurrent
new(firstcurrent, secondcurrent) =

Counter(4)
then

if firstcurrent > secondcurrent then

current <- firstcurrent + secondcurrent

else
current <-

firstcurrent - secondcurrent
member obj.Current = current
end

let

counter = new Counter(100)
let integer = counter.Current
System.Console.ReadKey()

|> ignore

By on 5/31/2010 8:09 AM ()

You cannot refer to let bindings in additional object constructors.

1
2
3
4
5
6
7
8
9
10
11
12
13
type Counter(anothercurrent) as self =
    [<DefaultValue()>]
    val mutable current : int       
    do self.current <- anothercurrent
    new(firstcurrent, secondcurrent) as self =
                                    Counter(4)
                                        then
                                        if firstcurrent > secondcurrent then
                                            self.current <- firstcurrent + secondcurrent
                                        else
                                            self.current <- firstcurrent - secondcurrent

    member obj.Current = self.current
By on 5/31/2010 8:53 AM ()

In your example, current field is a public one.
My intention was to have current as a private field, inaccessible outside the class.
That is why I've used a let binding.
But it's OK.
I'll find a solution for that.
Thanks.

By on 6/1/2010 12:42 AM ()

you can add private modifier to current

1
val mutable private current : int     
By on 6/1/2010 1:15 AM ()

I thought let binding makes a field private, and a val field is public by default.
But it seems there are more possibilities then that.:)

By on 6/1/2010 1:37 AM ()

That is what I needed.:)
Thanks.

By on 6/1/2010 1:34 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper