Is it possible to attach alerts to structure items?

Trying to add an alert to a regular let binding:

let foo = 1 [@@alert hey "there"]

But it has no effect.

One can work around like this (I can’t be held responsible for any eye-rolling or horrified expressions that may follow):

include (
  struct
    let foo = 1
  end :
    sig
      val foo : int [@@alert hey "there"]
    end)

Looking at the docs, it seems that alerts are only for types or signature items. But I wonder if this is a fundamental technical limitation? Or a design decision?

The following seems to work:

let foo [@alert hey "there"] = 1 

let x = foo + 1

which yields:

File "a.ml", line 3, characters 8-11:
3 | let x = foo + 1
            ^^^
Alert hey: foo
there

But I’m really not sure that it’s what I think it is. I just reduced the number of @s incrementally until I found one that worked :sunglasses:.

3 Likes

Wow! I tried a lot of combinations, but I guess not this one :smile: Thanks!

The attributes page has an explanation and some examples of where and how the different incantations of [@@@ your_attribute_goes_here] apply. Worth checking out!