Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
F Sharp (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Object-oriented programming=== Like other [[Common Language Infrastructure]] (CLI) languages, F# can use CLI types through object-oriented programming.<ref name="overview"/> F# support for object-oriented programming in expressions includes: * Dot-notation, e.g., {{code|lang=fsharp|code=x.Name}} * Object expressions, e.g., {{ code|lang=fsharp| code={ new obj() with member x.ToString() = "hello" } }} * Object construction, e.g., {{code|lang=fsharp|code=new Form()}} * Type tests, e.g., {{code|lang=fsharp|code=x :? string}} * Type coercions, e.g., {{code|lang=fsharp|code=x :?> string}} * Named arguments, e.g., {{code|lang=fsharp|code=x.Method(someArgument=1)}} * Named setters, e.g., {{code|lang=fsharp|code=new Form(Text="Hello")}} * Optional arguments, e.g., {{code|lang=fsharp|code=x.Method(OptionalArgument=1)}} Support for object-oriented programming in patterns includes * Type tests, e.g., {{code|lang=fsharp|code=:? string as s}} * Active patterns, which can be defined over object types<ref name="activePatterns">{{cite web |url=http://www.developerfusion.com/article/133772/pattern-matching-in-f-part-2-active-patterns/ |title=Pattern Matching in F# Part 2 : Active Patterns |access-date=2012-11-24 |archive-date=2012-12-09 |archive-url=https://web.archive.org/web/20121209125518/http://www.developerfusion.com/article/133772/pattern-matching-in-f-part-2-active-patterns |url-status=dead}}</ref> F# object type definitions can be class, struct, interface, enum, or delegate type definitions, corresponding to the definition forms found in [[C Sharp (programming language)|C#]]. For example, here is a class with a constructor taking a name and age, and declaring two properties. <syntaxhighlight lang="fsharp"> /// A simple object type definition type Person(name : string, age : int) = member x.Name = name member x.Age = age </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)