One of those things that I keep searching on the internet, for examples and Howto’s is, how do you hide stuff in Fsharp?

Hiding stuff can be very useful. When you create a library, or a DSL, or perhaps a framework, you want to provide a good user-experience by hiding implementation details. At the same time, you don’t want your code to add a magnitude of complexity, to fulfil this requirement. Fsharp is perfect for that, and the following may help.

Note that I will only handle cases which are harder to find on the internet, for basic syntax, please follow provided links.

Signature filesSignature files may be used to show or hide things at a file level. So if you have code file “mycode.fs”, you can have signature file “mycode.fsi”, which needs to come before the .fs file, in the file order of the project. So “mycode.fsi” specifies what can be seen in “mycode.fs”, by other files in the same project, and outside the project. What’s included in the .fsi will be accessible, what’s excluded will be hidden. You can automatically generate a signature file for the whole project, and use this as a start-point . I’ve seen issues with a generated signature file, so you still might need a bit of editing, but in most cases this is unnecessary.

Discriminated union
The cases of a discriminated union can either be fully hidden or fully public.
Example code:

From a different file, OpenDU.Case1 will be accessible, while ClosedDU.Op1 is not. So anything you want to do with ClosedDU.Op1 needs to be done in the .fs file.

Hiding in a record
A record may have record properties, but also static and instance members . A record may not inherit from another class (including an abstract class), but a record may implement an interface. So how do you show and hide all that?

Note that the first “type ClosedRecord” is to hide all record properties. The second line is to add the members to the type. Can’t you do that in one go? Good question, I did not find the answer. The “Create” member was added so that you can easily see that “this.Closed” is really hidden.

If a record implements an interface, you can hide it by excluding it from the signature file. You will get a warning that via reflection, one can still derive the interface. This is a design choice, if you hide to provide user experience, why care about someone digging with reflection?

Access Modifiers
Signature files are a nice way to hide and show large parts of your implementation. But sometimes you need it more fine grained.

If for example you have implemented an interface, or inherited from a baseclass (for classes), then you are required to include all interface or baseclass members in the signature file.

Sometimes that is more than you want to be public, because there can be technical reasons to choose for a baseclass/interface. For example, you want a set of types to implement a function member, while this function is only used internally. For user-experience, you don’t want to show that technical function to the outside world.
To hide such technical methods, you can use the “internal” access modifier on individual member functions, including “new “. You still need to mention all interface/inherited member functions in the signature.

Note that the “internal” modifier is applied on both the super- and the base-class. This way, member function “GetMyData” is hidden to the outside world, but accessible inside the project.
The outside world will see type “Base”, but that doesn’t have to be bad. You can also hide “Base”, but whether this is a good idea depends on your design.
​
You want to show a baseclass like “Base”, when you also implement types “StoreSomethingElse”, “StoreAnothingThing”, and you need to provide a combinator operator like this:

Short blog this time. I am introducing Websharper at my employer’s, but while sharing, I had a few startup problems. Probably, this information is already on the internet somewhere, but as it took me a while to solve these issues, I thought I might write a blog about it.

Coming from 2.5A few months ago, I installed Websharper 2.5.128.65. If you install Websharper today, you will download version 3.0.15.92-alpha. Unfortunately this contains breaking changes for your old code if you wrote any. Even some examples on Websharper’s website break because of the new version.

The markup types, like “Div” “Table” are not in the namespace “IntelliFactory.WebSharper.Html” anymore, these are now in the namespace “IntelliFactory.WebSharper.Html.Client”. Changing the namespace should do the trick.

Javascript logging was first done with a line like this:

Javascript.Log(“message”)

The new way to do this is:

A new FSharpThis usually applies for new projects. If you run a Websharper client, you may see a “Server Error” complaining that the Fsharp.Core cannot be found, and mentioning something like this:

JavaScript.Console.Log("message")

<%@Application Inherits="AdBongoDrum.YourGreatAppness"%>

There is a new Fsharp version on the block, and while Websharper 2.5 was still using 4.3.0, the newest version 3.0 is now standard using 4.3.1.

In the Web.config, you will find this section:

<!-- NOTE: comment the following to run on F# 3.0 --><runtime><assemblyBindingxmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentityname="FSharp.Core"publicKeyToken="b03f5f7f11d50a3a"culture="neutral"/><bindingRedirectoldVersion="0.0.0.0-4.3.1.0"newVersion="4.3.1.0"/></dependentAssembly></assemblyBinding></runtime>

You may comment out the full “runtime” section, in order to use Fsharp 4.3.0 again. This is a trick to get you quickly going again, without having to upgrade to the new Fsharp version.Cannot Add new Item﻿

This one got me worried about the stability of my work-laptop. At some point it is possible that when you want to add a new item, for example, a new .fs file, to you websharper project, that the window listing the code templates is empty. You can still add new items to purely F# projects, or C# projects, so this only happens for Websharper projects.

I am not sure how this situation occurs, but you will need to run this script to get your templates back again: script to recover templates

Also, this script is supplied in a Websharper project when you create a new project, under the name “Setup.fsx”.

Run this script as follows: open Setup.fsx, select all, click with right-mouse on the selected text and run it in the F# Interactive. After this operation, you should have your templates back again.

Remoting examples
Did you want to try one of the Websharper examples with remoting and got an HTTP 405? Then you probably took a “Single Page Application” and extended it with [<Rpc>] or [<Remote>] attributes to do something server side.

The problem is: there is no server. If you want to do anything on the server, then you require a Client Server Application. Halfway down the following page you find instructions how to run the remoting examples: Instruction remoting examples