Skip to content

Conversation

@miguel-s
Copy link
Collaborator

@miguel-s miguel-s commented May 7, 2021

Related to #340

@miguel-s miguel-s requested review from Malian, lnr0626 and msaraiva May 7, 2021 10:56
@Malian
Copy link
Collaborator

Malian commented May 7, 2021

@miguel-s I am torn between keeping the AST.If structure and adding a new AST.Unless structure

By using a new Unless struct will clarify the intent of the user and reduce cognitive overhead but I am totally fine with the If struct if we use the else attribute instead of reverting the condition.

I would also add tests in the if_test.exs to check the behaviour of #unless and not only the AST.

@miguel-s
Copy link
Collaborator Author

miguel-s commented May 7, 2021

@Malian great suggestions, thanks!

I am torn between keeping the AST.If structure and adding a new AST.Unless structure

My reasons for going with AST.If:

  1. Reusing existing code to speed up implementation, reduce our cognitive overhead and reduce maintenance costs
  2. Inspired by Elixir's unless, which uses and inverts if

But I'm not married to the idea :)

I would also add tests in the if_test.exs to check the behaviour of #unless and not only the AST.

Definitely. Wasn't very happy with my solution, so wanted to check my approach with you before investing more time into this.

miguel-s and others added 2 commits May 7, 2021 13:50
Co-authored-by: Malian <deronmalian@gmail.com>
@lnr0626
Copy link
Collaborator

lnr0626 commented May 7, 2021

I think sticking with ast.if makes sense for the reasons @miguel-s listed.

I'd originally thought inverting the condition was the way to go (inspired by elixir's unless), but using the else block is a clever solution.

miguel-s and others added 2 commits May 7, 2021 15:15
Co-authored-by: Lloyd Ramey <lnr0626@users.noreply.github.com>
@miguel-s miguel-s marked this pull request as ready for review May 7, 2021 13:19
@msaraiva
Copy link
Member

msaraiva commented May 7, 2021

One downside of using AST.If is that since we're generating an if instead of an unless, any related error presented will be mentioning if, not unless, which might be confusing. For instance:

if true do
  1
else
  2 
else
  3
end

will raise:

(ArgumentError) invalid or duplicate keys for if, only "do" and an optional "else" are permitted

I'm not sure if we're already validating duplicated else when handling AST.If. If we do and the message is generic (just mentioning else), then I guess we're good. After all, it's an edge case.

Regarding unless...else, I've been working with Elixir for 6 years and I never saw that in any code. However, since Elixir's unless have it, we should consider supporting it too. I'm not the best person to judge that as I never used unless in my life and my head already hurts reading unless alone. Reading unless...else just short-circuits my brain :)

@Malian
Copy link
Collaborator

Malian commented May 8, 2021

Regarding unless...else, I've been working with Elixir for 6 years and I never saw that in any code.

😱 I was glad to believe this did not exist!

However, since Elixir's unless have it, we should consider supporting it too. I'm not the best person to judge that as I never used unless in my life and my head already hurts reading unless alone. Reading unless...else just short-circuits my brain :)

I suggest not allow this at the first stage. If we receive feedback from users who want to add it, we will be able to consider supporting it too.

I'm not sure if we're already validating duplicated else when handling AST.If.

No we don't.

The following case is not validated too:

<#if {true}>
IF
<#else>
ELSE
<#elseif {true}>
ELSEIF
</#if>

@miguel-s
Copy link
Collaborator Author

miguel-s commented May 8, 2021

I suggest not allow this at the first stage. If we receive feedback from users who want to add it, we will be able to consider supporting it too.

Agree. It might be even positive to not support it. We can always revisit this it if we get enough feedback.

One downside of using AST.If is that since we're generating an if instead of an unless, any related error presented will be mentioning if, not unless, which might be confusing.

I've looked and not been able to find any case like this. @Malian can you think of any place this could happen?

@Malian
Copy link
Collaborator

Malian commented May 8, 2021

I've looked and not been able to find any case like this. @Malian can you think of any place this could happen?

With the current implementation I believe we cannot arrive in that specific invalid state. In the EEXEngine we have that piece of code that cannot have more than one if and one else option.

{:if, [generated: true], [condition, [do: if_buffer, else: else_buffer]]}

Nevertheless, a code that do not respect the if, if/else, if/elseif/elseif/..., if/elseif/.../elseif/else will fail at compile time with a useless message for the end-user:

** (KeyError) key :line not found in: %{}

@Malian
Copy link
Collaborator

Malian commented May 8, 2021

@miguel-s It is easy to validate both cases (see my branch https://github.com/Malian/surface/tree/validate-wrong-if-elseif-else-constructs).

That being said, I am not sure if we should merge it as it make the compiler file more complex and the work that @lnr0626 is doing on #378 will help us to have a more maintainable code, so I suggest to wait until #378 is merged before tackling the validations.

WDYT?

@msaraiva
Copy link
Member

msaraiva commented May 8, 2021

I suggest to wait until #378 is merge before tackling the validations.

Agreed. 👍

@miguel-s
Copy link
Collaborator Author

miguel-s commented May 8, 2021

I suggest to wait until #378 is merge before tackling the validations.

Sounds good to me.

@Malian @msaraiva what does it mean for this PR? Any changes needed, should I wait or ready to go?

Copy link
Member

@msaraiva msaraiva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miguel-s this is good to go!

@miguel-s miguel-s merged commit 77ff464 into surface-ui:surface-next May 10, 2021
lnr0626 added a commit to lnr0626/surface that referenced this pull request May 11, 2021
* surface-next:
  Remove `phx_` prefix from props (surface-ui#384)
  Remove code for embedded interpolation inside strings
  Introduce <#unless> (surface-ui#376)
  Introduce the concept of root properties (surface-ui#382)
  Warn on invalid prop/data defaults (surface-ui#374)
  Deprecated unquoted strings (surface-ui#379)
  Convert embedded interpolation (surface-ui#380)
  Introduce <#raw> and deprecate <#Raw> (surface-ui#377)
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

Successfully merging this pull request may close these issues.

4 participants