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

Syntax for trait uses clauses #36

Closed
kjx opened this issue Feb 10, 2016 · 9 comments
Closed

Syntax for trait uses clauses #36

kjx opened this issue Feb 10, 2016 · 9 comments

Comments

@kjx
Copy link
Contributor

kjx commented Feb 10, 2016

I was looking over James' notes from the Pomona meetings to see if I could find out what we said about the trait uses syntax.

The first option looks like this:

class list<T> {
    uses emptyness
    uses collectable

    var size is readable := 0
    method …
}

The second option looks like this

class list<T> {
    uses emptyness, collectable

    var size is readable := 0
    method …
}

The advantage of the first option, where each uses is separate, is that when the uses statements get long (with several alias and exclude clauses), they are clearly separated. The disadvantage is that they appear to be "executed" sequentially, rather than being composed and combined into the new object all at once. This syntax also introduces a new syntax error, when not all of the uses statements appear together.

The second option does a better job of expressing that all of the used traits are composed together "in parallel", but might get messy (requiring parenthesis and continuation lines) when there are several alias and exclude clauses on each.

Andrew

@kjx
Copy link
Contributor Author

kjx commented Feb 10, 2016

Because of alias and uses clauses, we should have separate top-level uses clauses.

Off the top of my head, I don't think we have any syntax anywhere that is a keyword followed by a comma-separate list of expressions.

@kjx
Copy link
Contributor Author

kjx commented Feb 16, 2016

  class someRandomClass {
      inherits someSuperClass
         excludes hash
         alias supera(x) b(y) = a(x) b(y) 
      uses someTrait
         excludes hash
         alias traita(x) b(y) = a(x) b(y) 
      uses someOtherTrait
         excludes hash
         alias traitTwoa(x) b(y) = a(x) b(y) 

  method hash is override { ...  }   //error?

  method a(x) b(y) {
     traita(x) b(y)
     supera(x) b(y)
     traitTwoa(x) b(y) 
  }     
}

@apblack apblack changed the title Traits 'uses' clauses Syntax for trait uses clauses Feb 17, 2016
@apblack
Copy link
Contributor

apblack commented Feb 17, 2016

Here is my suggestion from the original post:

The first option (multiple uses clauses) looks like this

class list<T> {
    inherits collection exclude size
    uses emptyness alias eTIsEmpty = isEmpty exclude sizeifUnknown(_)
    uses collectable alias collTIsEmpty = isEmpty exclude lazyFilter(_)

    var size is readable := 0
    method isEmpty { eTIsEmpty && collTIsEmpty }
    method …

}

The second option has one uses clause, and looks like this

class list<T> {
    inherits collection exclude size
    uses 
        emptyness alias etIsEmpty = isEmpty exclude sizeIfUnknown(_),
        collectable alias collIsEmpty = isEmpty exclude lazyFilter(_)

    var size is readable := 0
    method isEmpty { etIsEmpty && collIsEmpty }
    method …
}

Note that in this second example, the uses statement is a single statement on one logical line.
I've chosen to use layout to make it more readable, which I think is what I would teach students to do,
but there is nothing to stop them writing it all on a single line.

If we allow multiple comma-separated aliases, then they would need to be parenthesized to avoid ambiguity.

@kjx
Copy link
Contributor Author

kjx commented Feb 17, 2016

The real question is: how does either perform when things get even bigger:

  trait someRandomClass {
      use someSuperClass
         exclude hash
         exclude !=(x)
         exclude ==(x) 
         alias supera(x) b(y) = a(x) b(y) 
         alias supera(x) c(y) = a(x) d(y) 
         alias supera(x) d(y) = a(x) d(y) 
      use someOtherSuperClass
         exclude hash
         exclude !=(x)
         exclude ==(x) 
         alias supera(x) b(y) = a(x) b(y) 
         alias supera(x) c(y) = a(x) d(y) 
         alias supera(x) d(y) = a(x) d(y) 

vs

  trait someRandomClass {
      use someSuperClass, excludes hash, !=(x), ==(x), alias supera(x) b(y) = a(x) b(y),  supera(x) c(y) = a(x) d(y) supera(x) d(y) = a(x) d(y) 
      use someSuperClass, excludes hash, !=(x), ==(x), alias supera(x) b(y) = a(x) b(y),  supera(x) c(y) = a(x) d(y) supera(x) d(y) = a(x) d(y) 
      use someOtherSuperClass

I thought we talked about this somewhere else too: the point being that if we want to support multiple keywords (for use, exclude, alias) in these clauses, it's simple not to also support lists in that keyword.
It is just a matter of syntax, but every little bit is a little bit more, and then we have to provide guidance on which to pick etc etc etc. .

@kjx kjx added the 1:now label Feb 17, 2016
@apblack
Copy link
Contributor

apblack commented Feb 22, 2016

OK, you have persuaded me to allow multiple uses, each with a single trait.

But you have raised other issues: should we allow multiple alias clauses, or just one? Should we allow multiple exclude clauses, or just one? Must alias come before exclude (my assumption), or exclude before alias (your assumption), or is the order semantically relevant and therefore up to the programmer?

@kjx
Copy link
Contributor Author

kjx commented Feb 23, 2016

multiple uses, each with a single trait

and not multiple traits per uses?

should we allow multiple alias clauses, or just one? Should we allow multiple exclude clauses, or just one?

some of this is in #43. The order probably is relevant. I think it needs to be alias then exclude, so you can exclude the name you just aliased. But I'm not sure.

@kjx kjx closed this as completed Feb 23, 2016
@kjx
Copy link
Contributor Author

kjx commented Feb 23, 2016

multiple uses, each with a single trait, not multiple traits per uses.

@kjx
Copy link
Contributor Author

kjx commented Feb 24, 2016

so according to the protocol I wrote last week, this shouldn't be closed, but should be moved from "now" to "resolved, to be specd". which I've now done. sorry.

@kjx
Copy link
Contributor Author

kjx commented Mar 7, 2016

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

No branches or pull requests

2 participants