Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let and Property Assignment in one line

How can you write the following F# code (or similar) in one line:

let contextMenu = new ContextMenuStrip()
mainForm.ContextMenuStrip <- contextMenu

I have to declare contextMenu as it will be needed later.

like image 370
aann Avatar asked Nov 16 '09 14:11

aann


1 Answers

I don't recommand you to write it on a single line because this means it will be a mix between the #light (the mode by default now) and non #light syntax. If you really need to, you can use ;; like that:

open System
open System.Windows.Forms

let mainForm = new Form()
let contextMenu = new ContextMenuStrip();; mainForm.ContextMenuStrip <- contextMenu;;

If your expressions have unit type you can use a Sequential Execution Expression, which is an expression of the form:

expr1; expr2; expr3

for instance:

mainForm.ContextMenuStrip <- contextMenu; 5 + 6 |> ignore; mainForm.ContextMenuStrip <- null

I'd like to add that Sequential Execution Expressions have nothing to do with the non #light mode. They are just a general language construct.

Hope it helps.

like image 193
Stringer Avatar answered Sep 29 '22 11:09

Stringer