4

Xamarin/Android: F# scoping - how do I see a namespace in a different file?

I know this sounds really basic, but I can't seem to get it to work. I will illustrate with an example:

I start a new solution, I select a new F# Android App and call it FSScopeTest1, giving me MainActivity.fs

namespace FSScopeTest1
open System
open Android.Content
open Android.OS
open Android.Runtime
open Android.Views
open Adroid.Widget

[<Activity (Label = "FSScopeTest1", MainLauncher = true)>]
type MainActivity () =
    inherit Activity ()
    let mutable count:int = 1
    override this.OnCreate (bundle) =
        base.OnCreate (bundle)
        // Set our view from the "main" layout resource
        this.SetContentView (Resource_Layout.Main)
        // Get our button from the layout resource, and attach an event to it
        let button = this.FindViewById<Button>(Resource_Id.myButton)
        button.Click.Add (fun args ->
            button.Text <- sprintf "%d clicks" count
            count <- count + 1
        )

I then add a new F# Source file, ScopeTestNS.fs

namespace ScopeTestNS

module ScopeTestMod =
    let astr = "some text"

I then add to MainActivity.fs, on the second line:

open ScopeTestNS

and change the lamba expression for button.Click.Add to read

        button.Click.Add (fun args ->
            // button.Text <- sprintf "%d clicks!" count
            // count <- count + 1
            button.Text <- ScopeTestMod.astr
        )

now, when I build the solution, I get the error:

The namespace or module "ScopeTestMod" is not defined.

How do I make my namespace ScopeTestNS visible in MainActivity.fs so I can see any modules I define there?

Many thanks, Ross

  • 1
    Are the files in the correct order - The ScopeTestNs file needs to be included before the other file. – John Palmer Jun 17 '13 at 11:18
  • To amplify on @JohnPalmer's remarks; you need to manually change the compile order in the .fsproj file. This is a known issue with the F# binding. https://github.com/fsharp/fsharpbinding/issues/135 – Onorio Catenacci Jun 17 '13 at 16:59
  • And to be even more specific, "manual" here means open the fsproj file in a text editor. There's an ordering thing somewhere in a options menu (that looks kind of manual); don't bother with it. – James Moore Aug 21 '13 at 16:10

1 Answers1

5

The f# compiler reads the source files in a specific order (unlike the c# compiler). You need to ensure that your project has included the files in the correct order so that you can access modules defined in other files.

EDIT based on comment from Onorio Catenacci:

Apparently the F# binding used in Xamarin Studio does not currently support reordering of files in a F# project (see this github issue https://github.com/fsharp/fsharpbinding/issues/135). However, .fsproj files are simple xml files which can be edited with a text editor to change the order in which files are compiled.

EDIT EDIT

Apparently the F# addin in xamarin studio does now allow for drag and drop to reorder files (thanks to @7sharp9 for the update)

John Palmer
  • 25,356
  • 3
  • 48
  • 67
  • 1
    Old answer, but just to note that the F# addin does now let you reorder the files via drag and drop in Xamarin Studio. – 7sharp9 Apr 01 '14 at 09:24