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

Struct Literals #201

Closed
cjllanwarne opened this issue Mar 13, 2018 · 5 comments
Closed

Struct Literals #201

cjllanwarne opened this issue Mar 13, 2018 · 5 comments

Comments

@cjllanwarne
Copy link
Contributor

cjllanwarne commented Mar 13, 2018

Especially when working with structs, needing to type in object at struct-construction time is slightly counter-intuitive.

As I was writing some of these out in test cases, I was thinking about more intuitive struct literals. Here are a few options:

  • They all use A as the constructor indicator instead of object.
  • 1 and 2 use {}, 3 and 4 ()
  • 1 and 3 use variable names (eg i: ..., f:...), 2 and 4 do not.
struct A {
  Int i
  Float f
}
workflow foo {
  # Today's syntax:
  A a = object { i: 5, f: 5.5 }

  # Proposal 1:
  A a = A { i: 5, f: 5.5 }
  # Proposal 2:
  A a = A { 5, 5.5 }

  # Proposal 3:
  A a = A(i: 5, f: 5.5)
  # Proposal 4:
  A a = A(5, 5.5)
}

Any other thoughts?


Side note: why do we need anything at all before the { at all? Because otherwise the expression evaluates as a map literal and the key is actually an expression eg:

String i = "i"
String f = "g"

# oops, the 'f' key is actually "g", but we can't tell that until runtime:
A a = { i: 5, f: 5.5 }

NB: I think this side-note could lead to unexpected errors for authors. In the winstanley IDE I intend to flag the above pattern (ie a Struct value being assigned from a map literal) as a warning with the suggestion being "use an object/struct literal instead".

@orodeh
Copy link
Contributor

orodeh commented Mar 13, 2018

@cjllanwarne I am for 1 or 2.

@orodeh
Copy link
Contributor

orodeh commented Mar 15, 2018

Having chewed on it some more, I think braces (,) would be similar to object construction in standard programming languages. So, proposals 3 or 4. Another possibility:

# Proposal 1+2+3+4
A a = A( {i: 4, f: 5.5} )

We might want to allow all (or some) of these syntaxes. One way or the other, I suggest sticking to a known constructor syntax.

@patmagee
Copy link
Member

Good suggestions @cjllanwarne. I like proposal 3 and proposal 1. leading with the property you are setting is clearer to me in this case and allows some flexibility in terms of order when creating a struct. Additionally, I think these work better for complex type, since it becomes clearer what property is being set. Whereas leaving out the property it looks a bit muddled imo.

struct A {
  Array[Pair[String,File]] complex
  Int i
}

workflow foo {
  #Proposal 1
  A a = A { complex: [{left:"string",right: "gs://abc"}, {left:"string", right:"gs://cba"}], i: 1231 }
  #Proposal 3
  A a = A ( complex: [{left:"string",right: "gs://abc"}, {left:"string", right:"gs://cba"}], i: 1231 )

  #Proposal 2 (Dont like this as much)
  A a = A {  [{left:"string",right: "gs://abc"}, {left:"string", right:"gs://cba"}], 1231 }
  #Proposal 4 (Dont like this either)
  A a = A ([{left:"string",right: "gs://abc"}, {left:"string", right:"gs://cba"}], 1231 )
}

@patmagee
Copy link
Member

@cjllanwarne not sure if you have had time to look at this, but WDYT?

@patmagee
Copy link
Member

implemented by #297

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

3 participants