En3Tho / PoshRedisViewer

A compact redis browser for powershell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Documentation, sort of] I don't understand this code block

jmhickman opened this issue · comments

Hello there, thanks for reading. I found your project via its link on the terminal.gui project readme. I was reading the source code in order to understand how to use terminal.gui from F# in an idiomatic way.

I'm quite stuck on one section of your code though, and I was hoping you could explain how it worked to me.

The section is here, where as far as I can tell, a hierarchical relationship is being set up for all of the elements you created in makeViews().

I'm guessing this is a computation expression from your own extensions library? If so, could you point out which functionality in your library is being used so I can study it? I appreciate it.

@jmhickman Yes, this is a computation expression. It is coming from here:
https://github.com/En3Tho/FSharpExtensions/blob/main/FSharpExtensions/En3Tho.FSharp.ComputationExpressions/GenericCollectionCodeExtensions.fs

https://github.com/En3Tho/FSharpExtensions/blob/main/FSharpExtensions/En3Tho.FSharp.ComputationExpressions/CollectionBuilderExtensions.fs

In F# you can use any type as a computation expression if compiler can recognize a certain set of members on it.

First file declares a set of extensions to enable general computation expressions based on 'T -> unit type of code. E.g. for collections that would be something list List.Add(value). They define general things like usings, for-loops inside computation expressions.

Second file defines how an actual type will use that code. E.g. for collections we need to declare Yield and Run for the overall CE to run. For example Dictionary behaves differently from List and those extensions basically outline these differences. Other notable thing is declaring a generic "Run" member on any type can break other CEs that's why it is declared separately for each specific type.

In this library a "Run" is declared here: https://github.com/En3Tho/PoshRedisViewer/blob/main/PoshRedisViewer/PoshRedisViewer/UIUtil.fs#L62

And "Yield" is imported from more general "SCollectionBuilder" coming from that other library. It operates on any type that has "Add": 'T -> unit member.

Thank you so much for taking the time to answer me!

You're welcome :) F# CE's are fun and have great capabilities. I have more of them in that library of mine if you're interested. Like code builder for example