Hi Cameron,

virtual = abstract + default, e.g. try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 

#light

type Cat(name) = 
    abstract Name : string
    default x.Name = name

type DomesticCat(name,tame) = 
    inherit Cat(name)
    abstract Tame : bool
    default x.Tame = tame

I've omitted the setters for the properties, since often you won't need those.

Kind regards

don

By on 5/22/2008 10:55 PM ()

Hi Don,

Thanks for the reply, that clears up a couple of things. BTW, your Expert F# book is awesome! Compared to the C# books I picked up at the same time, the book is physically much smaller, but I think it is simply elegant and concise like the F# language.

I've found F# Interative to be a great tool for learning the .NET API and testing out various .NET libraries. I'm really looking forward to the Visual Studio work the team is working on. There are several IDE enhancements that I would like to see to make F# easier to develop with. Where would be the best place to give feedback?

The Cat and DomesticCat sample you provided appears to use the constructed class types that you talk about in chapter 6 of your book. In object oriented programming, plain old Java/CLR objects are often used. They have default constructors and mutable properties. The often have convenience constructors to. Libraries like NHibernate need types like this in order to create proxies. How can I create a type with a default constructor and abstract mutable properties?

cheers,
Cameron

By on 5/23/2008 11:58 AM ()

How can I create a type with a default constructor and abstract mutable properties?

I was hoping this might work:

type Asset() =
//let mutable id = ""
//member x.Id with get() = id and set(v) = id <- v
abstract Id : string
default x.Id with get() = id and set(v) = id <- v

but I get these errors:

  • No abstract property was found that corresponds to this override.
  • The value or constructor 'id' is not defined.

The C# equivelent of what I want to do looks like this:

public class Asset
{
private string id;
public virtual string Id
{
get { return id; }
set { id = value; }
}
}

Any idea what I'm doing wrong?

Cameron

By on 5/27/2008 9:17 AM ()
1
2
3
4
type Asset() =
  let mutable id = ""
  abstract Id : string with get, set
  default x.Id with get() = id and set(v) = id <- v
By on 5/27/2008 10:26 AM ()

Hopefully the last issue is the visibility of the 'id' variable. It feels like a F# bug, but I'm not sure. Not using #light syntax, when I try this:

1
2
3
4
5
  type Asset() = class
    let mutable private id : string = null
    abstract Id : string with get, set
    default x.Id with get() = id and set(v) = id <- v
  end

I get this error: Multiple visibility attributes have been specified for this identifier. 'let' bindings in classes are always private, as our let binding inside expressions.

If I remove the 'private' keyword, it compiles, but the C# signature is:

1
internal string _id@6;

Is this an F# (1.9.4.17) bug and/or is there a work-a-round?

Cameron

By on 5/27/2008 3:43 PM ()
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