1

I tried creating a literal for HWND_BROADCAST ((HWND)0xffff), which is marshalled as an IntPtr.

let [<Literal>] HWND_BROADCAST = 0xFFFFn

Visual Studio tells me: error FS0267: This is not a valid constant expression or custom attribute value.

Very well. Then I checked the documentation on F# Literals on MSDN, and I noticed that unativeint (UIntPtr) is on the list. I could use that in place of IntPtr. But:

let [<Literal>] HWND_BROADCAST = 0xFFFFun

Same error message.

So I have two questions:

  1. Why are nativeint literals not supported? Is there a reason behind it?
  2. Why doesn't the unativeint literal work, even though MSDN says it should?

Using F# 3.1.2 (F# Interactive version 12.0.30815.0) on Visual Studio 2013.


I noticed bigint is also on the list, which doesn't work either. So I'm guessing it's just MSDN being wrong.

Nikon the Third
  • 2,771
  • 24
  • 35

1 Answers1

2

Those are two separate things - the list contains all the literal strings that are supported, but not all of them can be marked with Literal attribute. I think the wording of the comment below the table is a bit unfortunate.

BigInt and IntPtr are structs, and that might be why they can't be declared as consts (I believe this is what the attribute boils down to). It seems that this is the case in C# as well, compare here.

I think the point holds for IntPtr, but BigInt could probably be treated specially by the compiler. Though usefulness of such a hack might be limited.

Community
  • 1
  • 1
scrwtp
  • 13,437
  • 2
  • 26
  • 30