Skip to content

Commit 2ef6c25

Browse files
committed
refactor: F# 8 lambda syntax and other simplifications
1 parent b9be1fe commit 2ef6c25

File tree

17 files changed

+71
-76
lines changed

17 files changed

+71
-76
lines changed

src/Bolero.Html/Builders.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type ChildContentAttr = delegate of receiver: obj * builder: RenderTreeBuilder *
4444
/// <param name="receiver">The containing component.</param>
4545
/// <param name="builder">The rendering builder.</param>
4646
/// <param name="sequence">The rendering sequence number.</param>
47-
type ChildAndRefContent = delegate of receiver: obj * builder: Rendering.RenderTreeBuilder * sequence: int -> int
47+
type ChildAndRefContent = delegate of receiver: obj * builder: RenderTreeBuilder * sequence: int -> int
4848

4949
type [<Struct; NoComparison; NoEquality>] AttrBuilder =
5050
member inline _.Yield([<InlineIfLambda>] attr: Attr) = attr

src/Bolero.Server/Components.fs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ open System.Threading.Tasks
3131
open Microsoft.AspNetCore.Components
3232
#if NET8_0
3333
open Microsoft.AspNetCore.Components.Endpoints
34-
open Microsoft.AspNetCore.Components.Web
3534
#endif
3635
open Microsoft.AspNetCore.Components.RenderTree
3736
open Microsoft.AspNetCore.Components.Rendering

src/Bolero.Server/Extensions.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type ServerComponentsExtensions =
8080
/// <param name="page">The page to return.</param>
8181
[<Extension>]
8282
static member BoleroPage(_this: Controller, page: Node) =
83-
new BoleroPageResult(page)
83+
BoleroPageResult(page)
8484

8585
/// <summary>Render the JavaScript tag needed by Bolero in a Razor page.</summary>
8686
/// <param name="config">The injected Bolero hosting configuration.</param>

src/Bolero.Server/Remoting/Endpoints.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ type internal RemotingEndpointDataSource() =
182182

183183
override _.Endpoints =
184184
endpointBuilders
185-
|> Seq.collect (fun b -> b.ApplyFinally())
186-
|> Seq.map (fun b -> b.Build())
185+
|> Seq.collect _.ApplyFinally()
186+
|> Seq.map _.Build()
187187
|> Array.ofSeq
188188
:> IReadOnlyList<Endpoint>
189189

src/Bolero.Templating.Provider/CodeGen.fs

+35-35
Original file line numberDiff line numberDiff line change
@@ -31,97 +31,97 @@ open Bolero.TemplatingInternals
3131
open Bolero.Templating.ConvertExpr
3232

3333
let getThis (args: list<Expr>) : Expr<TemplateNode> =
34-
TExpr.Coerce<TemplateNode>(args.[0])
34+
TExpr.Coerce<TemplateNode>(args[0])
3535

3636
let MakeCtor (holes: Parsing.Vars) =
3737
ProvidedConstructor([], fun args ->
3838
let holes = TExpr.Array<obj> [
3939
for KeyValue(_, type') in holes ->
4040
match type' with
41-
| Parsing.HoleType.String -> <@ box "" @>
42-
| Parsing.HoleType.Html -> <@ box (Node.Empty()) @>
43-
| Parsing.HoleType.Event _ -> <@ box (Events.NoOp<EventArgs>()) @>
44-
| Parsing.HoleType.DataBinding _ -> <@ box (null, Events.NoOp<ChangeEventArgs>()) @>
45-
| Parsing.HoleType.Attribute -> <@ box (Attr.Empty()) @>
46-
| Parsing.HoleType.AttributeValue -> <@ null @>
47-
| Parsing.HoleType.Ref -> <@ null @>
41+
| HoleType.String -> <@ box "" @>
42+
| HoleType.Html -> <@ box (Node.Empty()) @>
43+
| HoleType.Event _ -> <@ box (Events.NoOp<EventArgs>()) @>
44+
| HoleType.DataBinding _ -> <@ box (null, Events.NoOp<ChangeEventArgs>()) @>
45+
| HoleType.Attribute -> <@ box (Attr.Empty()) @>
46+
| HoleType.AttributeValue -> <@ null @>
47+
| HoleType.Ref -> <@ null @>
4848
]
4949
<@@ (%getThis args).Holes <- %holes @@>)
5050

5151
/// Get the argument lists and bodies for methods that fill a hole of the given type.
52-
let HoleMethodBodies (holeType: Parsing.HoleType) : (ProvidedParameter list * (Expr list -> Expr)) list =
52+
let HoleMethodBodies (holeType: HoleType) : (ProvidedParameter list * (Expr list -> Expr)) list =
5353
let (=>) name ty = ProvidedParameter(name, ty)
5454
match holeType with
55-
| Parsing.HoleType.String ->
55+
| HoleType.String ->
5656
[
5757
["value" => typeof<string>], fun args ->
58-
<@@ box (%%args.[1]: string) @@>
58+
<@@ box (%%args[1]: string) @@>
5959
]
60-
| Parsing.HoleType.Html ->
60+
| HoleType.Html ->
6161
[
6262
["value" => typeof<string>], fun args ->
63-
<@@ box (Node.Text (%%args.[1]: string)) @@>
63+
<@@ box (Node.Text (%%args[1]: string)) @@>
6464
["value" => typeof<Node>], fun args ->
65-
<@@ box (%%args.[1]: Node) @@>
65+
<@@ box (%%args[1]: Node) @@>
6666
]
67-
| Parsing.HoleType.Event argTy ->
67+
| HoleType.Event argTy ->
6868
[
6969
["value" => EventHandlerOf argTy], fun args ->
70-
Expr.Coerce(args.[1], typeof<obj>)
70+
Expr.Coerce(args[1], typeof<obj>)
7171
]
72-
| Parsing.HoleType.DataBinding Parsing.BindingType.BindString ->
72+
| HoleType.DataBinding BindingType.BindString ->
7373
[
7474
["value" => typeof<string>; "set" => typeof<Action<string>>], fun args ->
75-
<@@ box (box (%%args.[1]: string), Events.OnChange(%%args.[2])) @@>
75+
<@@ box (box (%%args[1]: string), Events.OnChange(%%args[2])) @@>
7676
]
77-
| Parsing.HoleType.DataBinding Parsing.BindingType.BindNumber ->
77+
| HoleType.DataBinding BindingType.BindNumber ->
7878
[
7979
["value" => typeof<int>; "set" => typeof<Action<int>>], fun args ->
80-
<@@ box (box (%%args.[1]: int), Events.OnChangeInt(%%args.[2])) @@>
80+
<@@ box (box (%%args[1]: int), Events.OnChangeInt(%%args[2])) @@>
8181
["value" => typeof<float>; "set" => typeof<Action<float>>], fun args ->
82-
<@@ box (box (%%args.[1]: float), Events.OnChangeFloat(%%args.[2])) @@>
82+
<@@ box (box (%%args[1]: float), Events.OnChangeFloat(%%args[2])) @@>
8383
]
84-
| Parsing.HoleType.DataBinding Parsing.BindingType.BindBool ->
84+
| HoleType.DataBinding BindingType.BindBool ->
8585
[
8686
["value" => typeof<bool>; "set" => typeof<Action<bool>>], fun args ->
87-
<@@ box (box (%%args.[1]: bool), Events.OnChangeBool(%%args.[2])) @@>
87+
<@@ box (box (%%args[1]: bool), Events.OnChangeBool(%%args[2])) @@>
8888
]
89-
| Parsing.HoleType.Attribute ->
89+
| HoleType.Attribute ->
9090
[
9191
["value" => typeof<Attr>], fun args ->
92-
<@@ box (%%args.[1]: Attr) @@>
92+
<@@ box (%%args[1]: Attr) @@>
9393
["value" => typeof<list<Attr>>], fun args ->
94-
<@@ box (Attr.Attrs(%%args.[1]: list<Attr>)) @@>
94+
<@@ box (Attr.Attrs(%%args[1]: list<Attr>)) @@>
9595
]
96-
| Parsing.HoleType.AttributeValue ->
96+
| HoleType.AttributeValue ->
9797
[
9898
["value" => typeof<obj>], fun args ->
99-
<@@ %%args.[1] @@>
99+
<@@ %%args[1] @@>
100100
]
101-
| Parsing.HoleType.Ref ->
101+
| HoleType.Ref ->
102102
[
103103
["value" => typeof<HtmlRef>], fun args ->
104-
<@@ box (%%args.[1]: HtmlRef) @@>
104+
<@@ box (%%args[1]: HtmlRef) @@>
105105
]
106106

107-
let MakeHoleMethods (holeName: string) (holeType: Parsing.HoleType) (index: int) (containerTy: ProvidedTypeDefinition) =
107+
let MakeHoleMethods (holeName: string) (holeType: HoleType) (index: int) (containerTy: ProvidedTypeDefinition) =
108108
[
109109
for args, value in HoleMethodBodies holeType do
110110
yield ProvidedMethod(holeName, args, containerTy, fun args ->
111111
let this = getThis args
112-
<@@ (%this).Holes.[index] <- %%(value args)
112+
<@@ (%this).Holes[index] <- %%(value args)
113113
%this @@>) :> MemberInfo
114114
]
115115

116-
let MakeFinalMethod (filename: option<string>) (subTemplateName: option<string>) (content: Parsing.Parsed) =
116+
let MakeFinalMethod (filename: option<string>) (subTemplateName: option<string>) (content: Parsed) =
117117
ProvidedMethod("Elt", [], typeof<Node>, fun args ->
118118
let this = getThis args
119119
let directExpr =
120120
let vars = content.Vars |> Map.map (fun k v -> Var(k, TypeOf v))
121121
let varExprs = vars |> Map.map (fun _ v -> Expr.Var v)
122-
((0, ConvertNode varExprs (Parsing.Concat content.Expr) :> Expr), vars)
122+
((0, ConvertNode varExprs (Concat content.Expr) :> Expr), vars)
123123
||> Seq.fold (fun (i, e) (KeyValue(_, var)) ->
124-
let value = <@@ (%this).Holes.[i] @@>
124+
let value = <@@ (%this).Holes[i] @@>
125125
let value = Expr.Coerce(value, var.Type)
126126
i + 1, Expr.Let(var, value, e)
127127
)

src/Bolero.Templating.Provider/ConvertExpr.fs

+8-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let WrapExpr (innerType: Parsing.HoleType) (outerType: Parsing.HoleType) (expr:
6767
/// Map an expression's vars from its parent, wrapping the expression in let declarations.
6868
let WrapAndConvert (vars: Map<string, Expr>) (subst: list<Parsing.VarSubstitution>) convert expr =
6969
let vars, addLets = ((vars, id), subst) ||> List.fold (fun (vars, addLets) wrap ->
70-
let unwrapped = vars.[wrap.name]
70+
let unwrapped = vars[wrap.name]
7171
let wrapped = WrapExpr wrap.innerType wrap.outerType unwrapped
7272
let var = Var(wrap.name, TypeOf wrap.innerType)
7373
let addLets e = Expr.Let(var, defaultArg wrapped unwrapped, addLets e) |> Expr.Cast
@@ -84,7 +84,7 @@ let rec ConvertAttrTextPart (vars: Map<string, Expr>) (text: Parsing.Expr) : Exp
8484
| Parsing.PlainHtml text ->
8585
<@ text @>
8686
| Parsing.VarContent varName ->
87-
let e : Expr<obj> = Expr.Coerce(vars.[varName], typeof<obj>) |> Expr.Cast
87+
let e : Expr<obj> = Expr.Coerce(vars[varName], typeof<obj>) |> Expr.Cast
8888
<@ (%e).ToString() @>
8989
| Parsing.WrapVars (subst, text) ->
9090
WrapAndConvert vars subst ConvertAttrTextPart text
@@ -100,11 +100,11 @@ let rec ConvertAttrValue (vars: Map<string, Expr>) (text: Parsing.Expr) : Expr<o
100100
| Parsing.PlainHtml text ->
101101
box <@ text @>
102102
| Parsing.VarContent varName ->
103-
box vars.[varName]
103+
box vars[varName]
104104
| Parsing.Fst varName ->
105-
box (Expr.TupleGet(vars.[varName], 0))
105+
box (Expr.TupleGet(vars[varName], 0))
106106
| Parsing.Snd varName ->
107-
box (Expr.TupleGet(vars.[varName], 1))
107+
box (Expr.TupleGet(vars[varName], 1))
108108
| Parsing.WrapVars (subst, text) ->
109109
WrapAndConvert vars subst ConvertAttrValue text
110110
| Parsing.Attr _ | Parsing.Elt _ | Parsing.HtmlRef _ ->
@@ -119,11 +119,11 @@ let rec ConvertAttr (vars: Map<string, Expr>) (attr: Parsing.Expr) : Expr<Attr>
119119
let value = ConvertAttrValue vars value
120120
<@ Attr.Make name %value @>
121121
| Parsing.VarContent varName ->
122-
vars.[varName] |> Expr.Cast
122+
vars[varName] |> Expr.Cast
123123
| Parsing.WrapVars (subst, attr) ->
124124
WrapAndConvert vars subst ConvertAttr attr
125125
| Parsing.HtmlRef varName ->
126-
let ref = vars.[varName] |> Expr.Cast
126+
let ref = vars[varName] |> Expr.Cast
127127
<@ Ref.MakeAttr(%ref) @>
128128
| Parsing.Fst _ | Parsing.Snd _ | Parsing.PlainHtml _ | Parsing.Elt _ ->
129129
failwith $"Invalid attribute: {attr}"
@@ -144,7 +144,7 @@ let rec ConvertNode (vars: Map<string, Expr>) (node: Parsing.Expr) : Expr<Node>
144144
let children = TExpr.Array<Node> (Seq.map (ConvertNode vars) children)
145145
<@ Node.Elt name %attrs %children @>
146146
| Parsing.VarContent varName ->
147-
vars.[varName] |> Expr.Cast
147+
vars[varName] |> Expr.Cast
148148
| Parsing.WrapVars (subst, node) ->
149149
WrapAndConvert vars subst ConvertNode node
150150
| Parsing.Fst _ | Parsing.Snd _ | Parsing.Attr _ | Parsing.HtmlRef _ ->

src/Bolero.Templating.Provider/Parsing.fs

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ let ParseText (t: string) (varType: HoleType) : Parsed =
239239
let mutable vars = Map.empty
240240
for p in parse do
241241
if p.Index > lastHoleEnd then
242-
parts.Add(PlainHtml t.[lastHoleEnd..p.Index - 1])
243-
let varName = p.Groups.[1].Value
242+
parts.Add(PlainHtml t[lastHoleEnd..p.Index - 1])
243+
let varName = p.Groups[1].Value
244244
if not (Map.containsKey varName vars) then
245245
vars <- Map.add varName varType vars
246246
parts.Add(VarContent varName)
247247
lastHoleEnd <- p.Index + p.Length
248248
if lastHoleEnd < t.Length then
249-
parts.Add(PlainHtml t.[lastHoleEnd..t.Length - 1])
249+
parts.Add(PlainHtml t[lastHoleEnd..t.Length - 1])
250250
WithVars vars (parts.ToArray() |> List.ofSeq)
251251

252252
/// None if this is not a data binding.

src/Bolero.Templating.Provider/Path.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let Canonicalize (path: string) =
3333
let GetRelativePath (baseDir: string) (fullPath: string) =
3434
let rec go (thisDir: string) =
3535
if thisDir = baseDir then
36-
fullPath.[thisDir.Length + 1..]
36+
fullPath[thisDir.Length + 1..]
3737
elif thisDir.Length <= baseDir.Length then
3838
invalidArg "fullPath" $"'{fullPath}' is not a subdirectory of '{baseDir}'"
3939
else

src/Bolero/Router.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ type PageModel<'T> =
197197
prop.SetValue(this, value)
198198
#else
199199
member internal this.SetModel(value) =
200-
(Unsafe.AsRef<'T>(&this.Model)) <- value
200+
Unsafe.AsRef<'T>(&this.Model) <- value
201201
#endif
202202

203203
[<AutoOpen>]
@@ -299,7 +299,7 @@ module private RouterImpl =
299299
if getTag x = 0 then
300300
None
301301
else
302-
s.write (someDector x).[0]
302+
s.write <| (someDector x)[0]
303303
}, ValueSome none
304304
| false, _ -> fail (InvalidRouterKind.UnsupportedType ty)
305305
else

tests/Client/Main.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ let view js model dispatch =
347347
cond model.page <| function
348348
| Form -> viewForm js model dispatch
349349
| Collection -> viewCollection model dispatch
350-
| Item (k, m) -> ecomp<ViewItemPage,_,_> (k, model.items.[k], m.Model) dispatch { attr.empty() }
350+
| Item (k, m) -> ecomp<ViewItemPage,_,_> (k, model.items[k], m.Model) dispatch { attr.empty() }
351351
| Lazy (x, y) -> viewLazy (x, y) model dispatch
352352
| Virtual -> viewVirtual model dispatch
353353
| NotFound -> p { text "Not found" }

tests/Remoting.Client/Main.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ let Update (myApi: MyApi) msg model =
138138

139139
let router : Router<Page, Model, Message> =
140140
{
141-
getEndPoint = fun m -> m.page
141+
getEndPoint = _.page
142142
getRoute = function
143143
| Home -> "/"
144144
| Custom i -> $"/custom/{i}"
@@ -157,7 +157,7 @@ type Form = Template<"subdir/form.html">
157157
type Item() =
158158
inherit ElmishComponent<KeyValuePair<int, string>, Message>()
159159

160-
override __.View (KeyValue (k, v)) dispatch =
160+
override _.View (KeyValue (k, v)) dispatch =
161161
Tpl.item().key(string k).value(v)
162162
.remove(fun _ -> dispatch (RemoveItem k))
163163
.Elt()

tests/Remoting.Server/Startup.fs

+3-5
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ open Microsoft.AspNetCore
2727
open Microsoft.AspNetCore.Authentication.Cookies
2828
open Microsoft.AspNetCore.Builder
2929
open Microsoft.AspNetCore.Hosting
30-
open Microsoft.AspNetCore.Http
31-
open Microsoft.AspNetCore.Mvc
3230
open Microsoft.Extensions.DependencyInjection
3331
open Microsoft.Extensions.Hosting
3432
open Microsoft.Extensions.Logging
@@ -47,14 +45,14 @@ module Page =
4745
``base`` { attr.href "/" }
4846
}
4947
body {
50-
div { attr.id "main"; comp<Client.MyApp> }
48+
div { attr.id "main"; comp<MyApp> }
5149
script { attr.src "_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js" }
5250
boleroScript
5351
}
5452
}
5553

5654
type MyApiHandler(log: ILogger<MyApiHandler>, ctx: IRemoteContext) =
57-
inherit RemoteHandler<Client.MyApi>()
55+
inherit RemoteHandler<MyApi>()
5856

5957
let mutable items = Map.empty
6058

@@ -105,7 +103,7 @@ type Startup() =
105103
services.AddSwaggerForSystemTextJson(JsonFSharpOptions()) |> ignore
106104
services.AddEndpointsApiExplorer() |> ignore
107105

108-
member this.Configure(app: IApplicationBuilder, env: IHostEnvironment, log: ILogger<Startup>) =
106+
member this.Configure(app: IApplicationBuilder, env: IHostEnvironment) =
109107
app.UseAuthentication()
110108
.UseStaticFiles()
111109
.UseSwagger()

tests/Unit.Client/Html.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type BoleroComponent() =
7777
condUnionState <-
7878
match s.Length with
7979
| 0 -> Empty
80-
| 1 -> OneChar s.[0]
80+
| 1 -> OneChar s[0]
8181
| _ -> ManyChars s)
8282
}
8383
cond condUnionState <| function

tests/Unit/Fixture.fs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ open OpenQA.Selenium.Chrome
3131
// open OpenQA.Selenium.Firefox
3232
open OpenQA.Selenium.Support.UI
3333
open Swensen.Unquote
34-
open Bolero.Tests
3534

3635
/// Defines the setup/teardown for all tests that use the web server and Selenium.
3736
/// These web tests must be located in the namespace Bolero.Tests.Web

tests/Unit/Tests/Routing.fs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Bolero.Tests.Web
22

33
open System.Text.RegularExpressions
4-
open System.Threading
54
open FSharp.Reflection
65
open NUnit.Framework
76
open OpenQA.Selenium
@@ -88,19 +87,19 @@ module Routing =
8887

8988
let failingRouters = [
9089
failingRouter<``Invalid parameter syntax``> <| fun c ->
91-
InvalidRouterKind.ParameterSyntax(c.[0], "{x")
90+
InvalidRouterKind.ParameterSyntax(c[0], "{x")
9291
failingRouter<``Unknown parameter name``> <| fun c ->
93-
InvalidRouterKind.UnknownField(c.[0], "y")
92+
InvalidRouterKind.UnknownField(c[0], "y")
9493
failingRouter<``Duplicate field``> <| fun c ->
95-
InvalidRouterKind.DuplicateField(c.[0], "x")
94+
InvalidRouterKind.DuplicateField(c[0], "x")
9695
failingRouter<``Incomplete parameter list``> <| fun c ->
97-
InvalidRouterKind.MissingField(c.[0], "y")
96+
InvalidRouterKind.MissingField(c[0], "y")
9897
failingRouter<``Identical paths with different parameter names``> <| fun c ->
99-
InvalidRouterKind.IdenticalPath(c.[1], c.[0])
98+
InvalidRouterKind.IdenticalPath(c[1], c[0])
10099
failingRouter<``Mismatched type parameters in same position``> <| fun c ->
101-
InvalidRouterKind.ParameterTypeMismatch(c.[1], "y", c.[0], "x")
100+
InvalidRouterKind.ParameterTypeMismatch(c[1], "y", c[0], "x")
102101
failingRouter<``Rest parameter in non-final position``> <| fun c ->
103-
InvalidRouterKind.RestNotLast(c.[0])
102+
InvalidRouterKind.RestNotLast(c[0])
104103
failingRouter<Dictionary<int, int>> <| fun _ ->
105104
InvalidRouterKind.UnsupportedType typeof<Dictionary<int, int>>
106105
]

tests/Unit/Tests/Templating.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module Templating =
9696
Double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, ref 0.)
9797
let isValidPosition (pos: string) =
9898
let a = pos.Split(',')
99-
isNumber a.[0] && isNumber a.[1]
99+
isNumber a[0] && isNumber a[1]
100100

101101
elt.ByClass("btn1").Click()
102102
elt.Eventually <@ state.Text = "clicked 1" @>

0 commit comments

Comments
 (0)