Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if/else or match expressions with different structured state #4

Open
SchlenkR opened this issue May 3, 2023 · 1 comment
Open

if/else or match expressions with different structured state #4

SchlenkR opened this issue May 3, 2023 · 1 comment

Comments

@SchlenkR
Copy link
Collaborator

SchlenkR commented May 3, 2023

In (VideOMDbSample)[https://github.com/AngelMunoz/VideOMDbSample], there's an example that uses a match expression that doesn't compile:

      match currentPage.Value with
      | Pages.Movie id -> Pages.Movie.View id
      | Pages.Landing -> Pages.Landing.View()
      | Pages.Search -> Pages.Search.View()
      | Pages.Movie id -> Pages.Movie.View id

Like if/else expressions, every branch has to be of the "same type". This is propably something I have to explain and document, because it's not intuitive what "same type" means: It is the type Vide<'value, 'state, 'context> where value is the component's actual return value (usually unit ), but state is something that is propably not the same type when partial views differ.

There's an explanation that might help: https://vide-dev.io/docs/vide.fable/conditions/

Here's the comment of @AngelMunoz on his expectations:

Yes I expected that to work, even for the if/else case as it is a fairly common use case IMO, something that comes to my mind is “Routing” routers often return a different component based on what was resolved by the router, partial views are also something that comes to mind, e.g. user is authenticated show this, not authenticated show that.
how can we continue in these kinds of scenarios or how is the user expected to reduce dynamic views?

Since it's not possible to define custom handling for if/else or match expressions in the context of CEs, this issue must be documented in a very prominent way, or it could be handled with "conditional" and "witch" library functions that have to be used instead of if/else or match.

...and additionally:

I think control flow via well defined functions that tip the compiler is a good option I've seen forms of that in some frameworks (like solid js https://www.solidjs.com/docs/latest/api#control-flow) ...

@SchlenkR
Copy link
Collaborator Author

SchlenkR commented May 8, 2023

I came up with an implementation of switch / case that allows for

  • cases that show only when there's no previous case that was shown,
  • cases that always show (when their condition is met; independent of previous cases),
  • default case
  • specify if a case shall preserve it's state or reset it on show

Example:

vide {
    let! (viewNr : int) = chooseView

    // There are 3 views. The 2nd view's state is cleared on every show.
    // All other view retain their state.

    switch (fun x -> x = viewNr)
    |> case 0 componentWithBooleanState
    |> caseForget 1 componentWithIntState
    |> case 2 componentWithStringState
    |> caseDefault (div { "Nothing to show - this is the default case." })
}

More examples are here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant