Skip to content

Commit 5fcad29

Browse files
isaacabrahambaronfel
authored andcommitted
Update watcher.fsx to support nullness
I have zero experience on the Ionide codebase, but saw that VSCode was giving nullness warnings on starting FSI e.g. ``` ionide.ionide-fsharp-7.22.0\watcher\watcher.fsx(48,34): warning FS3261: Nullness warning: The types 'System.Reflection.MemberInfo' and 'System.Reflection.MemberInfo | null' do not have compatible nullability. ``` This PR removes the warnings, but someone should review them to make sure that there hasn't been any semantic change in behaviour.
1 parent 77e1b3f commit 5fcad29

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

release/watcher/watcher.fsx

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ fsi.AddPrinter (fun (_: obj) ->
22
let fsiAsm = "FSI-ASSEMBLY"
33

44
let asmNum (asm:System.Reflection.Assembly) =
5-
asm.GetName().Name.Replace(fsiAsm, "") |> System.Int32.TryParse |> fun (b,v) -> if b then v else 0
5+
match asm.GetName().Name with
6+
| null -> 0
7+
| name -> name.Replace(fsiAsm, "") |> System.Int32.TryParse |> fun (b,v) -> if b then v else 0
68

79
let fsiAssemblies =
810
// use multiple assemblies (FSI-ASSEMBLY1, FSI-ASSEMBLY2...) if single isn't found
911
let fsiAsms =
1012
System.AppDomain.CurrentDomain.GetAssemblies()
11-
|> Array.filter (fun asm -> asm.GetName().Name.StartsWith fsiAsm)
13+
|> Array.filter (fun asm ->
14+
match asm.GetName().Name with
15+
| null -> false
16+
| name -> name.StartsWith fsiAsm
17+
)
18+
1219
fsiAsms
1320
|> Array.tryFind (fun asm -> asm.GetName().Name = fsiAsm)
1421
|> function
@@ -30,9 +37,14 @@ fsi.AddPrinter (fun (_: obj) ->
3037
|> Seq.sortBy (fun (_, (i, _)) -> i) //order by original index
3138
|> Seq.map (fun (_, (_, pi)) -> pi.Name, pi.GetValue(null, Array.empty), pi.PropertyType) //discard ordering index, project usuable watch value
3239

40+
let isFsiAssembly (ty:System.Type) =
41+
match ty.FullName with
42+
| null -> false
43+
| name -> name.StartsWith("FSI")
44+
3345
let getRecords (fsiAssembly:System.Reflection.Assembly) =
3446
fsiAssembly.GetTypes()
35-
|> Seq.filter (fun ty -> ty.FullName.StartsWith("FSI"))
47+
|> Seq.filter isFsiAssembly
3648
|> Seq.filter (Reflection.FSharpType.IsRecord)
3749
|> Seq.map (fun ty ->
3850
let flds =
@@ -43,9 +55,12 @@ fsi.AddPrinter (fun (_: obj) ->
4355

4456
let getUnions (fsiAssembly:System.Reflection.Assembly) =
4557
fsiAssembly.GetTypes()
46-
|> Seq.filter (fun ty -> ty.FullName.StartsWith("FSI"))
58+
|> Seq.filter isFsiAssembly
4759
|> Seq.filter (Reflection.FSharpType.IsUnion)
48-
|> Seq.filter (fun ty -> ty.BaseType.Name = "Object") //find DU declaration not DU cases
60+
|> Seq.filter (fun ty ->
61+
match ty.BaseType with
62+
| null -> false
63+
| ty -> ty.Name = "Object") //find DU declaration not DU cases
4964
|> Seq.map (fun ty ->
5065
let flds =
5166
Reflection.FSharpType.GetUnionCases ty
@@ -60,7 +75,7 @@ fsi.AddPrinter (fun (_: obj) ->
6075

6176
let getFuncs (fsiAssembly:System.Reflection.Assembly) =
6277
fsiAssembly.GetTypes()
63-
|> Seq.filter (fun ty -> ty.FullName.StartsWith("FSI"))
78+
|> Seq.filter isFsiAssembly
6479
|> Seq.filter (Reflection.FSharpType.IsModule)
6580
|> Seq.choose (fun ty ->
6681
let meth =
@@ -97,10 +112,9 @@ fsi.AddPrinter (fun (_: obj) ->
97112

98113
let fromAssemblies folder =
99114
fsiAssemblies
100-
|> Array.toList
101-
|> Seq.map (fun asm -> asmNum asm, asm)
102-
|> Seq.sortByDescending fst // assume later/higher assembly # always shadows
103-
|> Seq.fold folder (Set.empty, Seq.empty)
115+
|> Array.map (fun asm -> asmNum asm, asm)
116+
|> Array.sortByDescending fst // assume later/higher assembly # always shadows
117+
|> Array.fold folder (Set.empty, Seq.empty)
104118
|> snd
105119

106120
let arrangeVars (state: Set<string> * seq<string>) (step, asm) =
@@ -123,7 +137,7 @@ fsi.AddPrinter (fun (_: obj) ->
123137
let shadowed = (fst state).Contains name
124138
let parms =
125139
parms
126-
|> Seq.map (fun (n, t) -> n + ": " + t)
140+
|> Seq.map (fun (n, t) -> $"{n}: {t}")
127141
|> String.concat "; "
128142
formatVarsAndFuncs name parms typ step shadowed, name)
129143
let names =
@@ -199,4 +213,4 @@ fsi.AddPrinter (fun (_: obj) ->
199213
}
200214
|> Async.Start
201215

202-
null)
216+
"")

0 commit comments

Comments
 (0)