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

Early return operator #3514

Open
berekuk opened this issue Jan 21, 2025 · 0 comments
Open

Early return operator #3514

berekuk opened this issue Jan 21, 2025 · 0 comments
Labels
Language Regarding Squiggle language semantics, distributions and function registry stage:design Needs more work to describe how it would work

Comments

@berekuk
Copy link
Collaborator

berekuk commented Jan 21, 2025

Discussed here: https://eaforecasting.slack.com/archives/C030T49UHSS/p1737486831253379

Long nested ifs in FP can get ugly.
I wonder if we could add some syntax for early returns, and keep the rest of Squiggle functional.

  agentFieldDistribution(agent, property) = {
    propertyValues = data[property]
    if typeOf(propertyValues) == "Dictionary" then {
      values = propertyValues[agent]
      if typeOf(values) == "Dictionary" then {
        propertyDistribution(values, property)
      } else "undefined"
    } else "undefined"
  }

->

  agentFieldDistribution(agent, property) = {
    propertyValues = data[property]    
    returnif typeOf(propertyValues) != "Dictionary" then "undefined"

    values = propertyValues[agent]
    returnif typeOf(values) != "Dictionary" then "undefined"

    propertyDistribution(values, property)
  }

One downside, as we discussed, is that this feature is pretty unique, i.e. I can't find any FP languages that have this.

On returnif [condition] then [expression]:

  • new keyword at the beginning is convenient for parsing. if ... then return ... would be more complicated, because the entire construct is not an expression, but a statement
  • the specific keywords could be different; maybe return [expression] if [condition]? (I think I prefer the condition -> expression order though)
@berekuk berekuk added Language Regarding Squiggle language semantics, distributions and function registry stage:design Needs more work to describe how it would work labels Jan 21, 2025
@github-project-automation github-project-automation bot moved this to 🆕 To prioritize in Overview Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Language Regarding Squiggle language semantics, distributions and function registry stage:design Needs more work to describe how it would work
Projects
Status: 🆕 To prioritize
Development

No branches or pull requests

1 participant