3

I am working though Petzold's Creating Mobile Apps Using Xamarin Forms book, translating the C# code into F# where the F# code is not available on GitHub (he stopped posting FS after chapter 7). In chapter 9, page 189, he makes use of the Dependency attribute like this:

[assembly: Dependency(typeof(DisplayPlatformInfo.iOS.PlatformInfo))]
namespace DisplayPlatformInfo.iOS
{
  public interface IPlatformInfo
 {
 string GetModel();
 string GetVersion();
 }
  using System;
 using UIKit;
 using Xamarin.Forms;
  public class PlatformInfo : IPlatformInfo
 {
 UIDevice device = new UIDevice();
 //etc...

I want to do the equivalent in F#. I created the type and the only place I can add that attribute is at a generic do() statement:

type PlatformInfo () =
    [<assembly: Dependency(typeof(Greetings.iOS.PlatformInfo))>]
    do()

    interface IPlatformInfo with
        member this.GetModel () = 
            let device = new UIDevice()
            device.Model.ToString()
        member this.GetVersion () = 
            let device = new UIDevice()
            String.Format("{0} {1}", device.SystemName, device.SystemVersion)

The problem is that I get a

warning: attributes are ignored in this construct.

How should I be placing this attribute into the type?

2 Answers 2

4

Assembly level attributes in F# need to be a in module, at the top level.

I would translate the C# above as:

namespace rec DisplayPlatformInfo.iOS

// Make a module specifically for this attribute
module DisplayPlatformAssemblyInfo =
    [<assembly: Dependency(typeof(Greetings.iOS.PlatformInfo))>]
    do ()

type IPlatformInfo =
    abstract member GetModel : unit -> string
    abstract member GetVersion : unit -> string

// ... Implement your type, etc
5
  • The error on the attribute is now "this is not a valid constant expression or custom attribute value". Jun 20, 2018 at 1:40
  • "The generic function "typeof" but be given explicit type arguments" is the runtime error Jun 20, 2018 at 1:42
  • the problem is that the type that implements the Interface needs to be defend before the module so the typeof <> can find it Jun 20, 2018 at 2:14
  • @Jamie dixon just put it at the end of the file (or use namespace rec) Jun 20, 2018 at 5:09
  • I wrote up what I found here: jamessdixon.wordpress.com/2018/06/20/… Jun 20, 2018 at 11:21
1

Since it is an assembly attribute, it should be place at the top level of a module instead of in a type. The following should be working:

[<assembly: Dependency(typeof(Greetings.iOS.PlatformInfo))>]
do ()

type PlatformInfo () =
   // ... 
3
  • I know, that's what I wrote! But you are right I should have make that more explicit in the code example. Yours is perfect! :-)
    – user9895453
    Jun 19, 2018 at 22:31
  • It's just that given is code I assumed implicit module: "If a code file does not begin with a top-level module declaration or a namespace declaration, the whole contents of the file, including any local modules, becomes part of an implicitly created top-level module".
    – user9895453
    Jun 20, 2018 at 4:57
  • Yes, but he needs this in a namespace for the attribute Jun 20, 2018 at 5:10

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.