title: Understanding F# type constructors
body: <p>I am trying to create a complex type and need, at construction time, to perform some actions.
  So I started writing my code:</p>
  
  <pre><code>type public MyType =
     val private myvar: int
     val private myvar2: string
     (* ...Some val declarations... *)
     new () = {
        (* The default ctor makes something *)
     }
     new (ctorpar: AnotherType) = {
        myvar = 1;
        myvar2 = "Hello";
        (* Other assignments in the form var = val *)
        (* Here I would like to start a cycle in order *)
        for i in ctorpar.array do
           (* Do something *) (* ERROR *)
     }
  </code></pre>
  
  <p>Well when trying to place a for or something else from an assignment the compiler goes mad.
  I assume the following: The syntax for new follows the one of Computational Expressions, or better, the new is a computational expression (I imply this because of the curly brackets and the semicolon from one oinstruction to another one). And in this case, for constructor computetional expressions, it is possible to palce assignments only.</p>
  
  <p>So please could you answer me:</p>
  
  <p>1) Is my deduction correct? (regarding computetional expressions and the constructor for types).</p>
  
  <p>2) What should I do whether I need to place an articulated set of instructions to be executed in the constructor???? Well you know, sometimes it is necessary to perform an action at construction time and it might involve everything from a cycle to all possibilities.</p>
  
  <p>But the compiler gets mad anyway...</p>
  
  <p>Thanks to kvd I understood that I have the possibility to do the following:</p>
  
  <pre><code>type public MyType =
     val private myvar: int
     val private myvar2: string
     (* ...Some val declarations... *)
     new () = {
        (* The default ctor makes something *)
     }
     new (ctorpar: AnotherType) = 
        for i in ctorpar.ACollection do
           (* Something *)
        {
        myvar = 1;
        myvar2 = "Hello";
        }
  </code></pre>
  
  <p>Well I am sorry, but this does not help me because F# compiler tells me this:</p>
  
  <blockquote>
    <p>Object constructors cannot directly
    use try/with and try/finally prior to
    the initialization of the object. This
    includes constructors such as 'for x
    in ...' that may elaborate to uses of
    these constructs. This is a limitation
    imposed by Common IL.</p>
  </blockquote>
  
  <p>OK, if the problem is doing something prior to the object initalization, and it sounds correct, then let's do this after:</p>
  
  <pre><code>type public MyType =
     val mutable private myvar: int
     val mutable private myvar2: string
     (* ...Some val declarations... *)
     new () = {
        (* The default ctor makes something *)
     }
     new (ctorpar: AnotherType) = 
        {
        myvar = 1;
        myvar2 = "Hello";
        }
        then
        for i in ctorpar.ACollection do
           (* Something *)
           myvar &lt;- 10
  </code></pre>
  
  <p>Again frustrated by failure:</p>
  
  <blockquote>
    <p>The value or constructor 'myvar' is
    not defined.</p>
  </blockquote>
  
  <p>What should I do????
  It seems that after, it does not recognize elements in my class, it seems correct because it would require an indentifier as for when declaring members using self or this... here it does not have a self-reference and, correctly, tells me: "You're trying to get something I cannot give you!!!!!!"</p>