syntax: Allow nesting If directives in Set directives #5

Open Jookia opened this issue on 24 Aug 2021 - 8 comments

@Jookia Jookia commented on 24 Aug 2021

Currently If directives discard their value unconditionally. This isn't very useful for actually getting work done in a language that will aim to be mostly functional.

I'm not too sure exactly how this would nest or even if this is the right thing to do, so I'm just going to throw out some ideas:

Set Day To If Christmas Then BeginText Christmas EndText Else BeginText Not Christmas EndText EndIf EndSet

This approach seems to be the easiest to define- just let if directives happen in set directives. But to break it up over lines it looks a lot stranger as you can no longer tell what's happening just by reading the start of the line.

Set Day To
If Christmas
Then BeginText Christmas EndText
Else BeginText Not Christmas EndText
EndIf
EndSet

Alternatively you could put the sets in the if directives like this:

If Christmas Then Set Day To BeginText Christmas EndText Else Set Day To BeginText Not Christmas EndText EndIf

This reads a bit better as:

If Christmas
Then Set Day To BeginText Christmas EndText
Else Set Day To BeginText Not Christmas EndText
EndIf

This would effectively make if directives a control structure, which makes sense to me.
However it's less safe: What if one branch doesn't set Day but we use it later?

We could try and use levels of nesting to make it easier for people to skim past:

Set Day To
Cont If Christmas
Cont Cont Then BeginText Christmas EndText
Cont Cont Else BeginText Not Christmas EndText
Cont EndIf
EndSet

Perhaps we could introduce some kind of indentation or nesting syntax?

Set Day To
In If ChristmasSet Day To 
In Then BeginText Christmas EndText
In Else BeginText Not Christmas EndText
In EndIf
EndSet

Another approach altogether is to bind the result to a variable automatically, like this:

If ChristmasThen BeginText Christmas EndText Else BeginText Not Christmas EndText EndIf
Set Day To Result EndSet

or written nicely:

If Christmas
Then BeginText Christmas EndText
Else BeginText Not Christmas EndText
EndIf
Set Day To Result EndSet

Where 'Result' would be the result of the if statement. I like this approach the most because it avoids nesting, which is a goal of the language. Maybe we could explain it by being a stack value or something?

I talked with Koz about this and looked at that last approach. The 'If ... Set' approach is the road to madness since 'Result' just comes out of nowhere.

But he did propose something like:

Set Day If Christmas
Then BeginText Christmas EndText
Else BeginText Not Christmas EndText
EndSetIf

This reads a little strange as if the set is conditional, but the value is conditional.

Mikoto suggested this:

Set Day To BeginText Christmas EndText If Christmas Else BeginTest Not Christmas EndText EndSet

This would unroll as:

Set Day To BeginText Christmas EndText
If Christmas
Else BeginTest Not Christmas EndText
EndSet

I must say I'm a bit confused by this, what it would do or and why. Can you explain better ?

So right now when you do an if statement, it runs an action for success or failure. But what if you want the result of that action? Right now the result gets discarded like a command directive, there's no way to give it a name like a set directive.

Here's a quick example: Let's say we want to add a number to someone's name if it's already taken.
We'd write it like this currently:

If NameDatabase Contains Name
Then Name Append BeginText 1234 EndText
Else Name
EndIf

But this doesn't actually do anything, because Append just creates a new value.
So in this case we'd want something like:

Set NewName If NameDatabase Contains Name
Then Name Append BeginText 1234 EndText
Else Name
EndSet

Where the value of NewName is set to either the result of Append or just Name again.

It might make more sense if you mentally replace Append with Join

Here's a quick question to myself: How does this interact with setting variables and returning?

Set X If .... EndSet is obviously the way to do set and is equivalent to ternary operations

But what about conditional returns? Where based on a result you either return or set a variable? A bit like

if(!(name = read_valid_name())) return;

I guess in NewLang that would be represented as:

Set Name To Read_Valid_Name EndSet
If Not Name Then Return EndIf

Note that these are NOT just nested if statements, as they don't support returning/jumping. Only values/function calls.

Just a quick note, If directives probably need prefixes like 'Not'

Labels

Priority
high
Milestone
No milestone
Assignee
No one assigned
2 participants
@Jookia @xogium