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

Feature Request: Object cloning in function parameters #161

Closed
dubiousdavid opened this issue Aug 29, 2012 · 5 comments
Closed

Feature Request: Object cloning in function parameters #161

dubiousdavid opened this issue Aug 29, 2012 · 5 comments

Comments

@dubiousdavid
Copy link

@dubiousdavid dubiousdavid commented Aug 29, 2012

It would be really cool to be able to declare a function parameter as an object that should be cloned, as opposed to passed by reference. It's really lame how Javascript co-mingles pass by value and pass by reference. Another option would be a flag to pass to the compiler, as I never want pass by reference in any LiveScript code.

billy = name: \Billy
name-to-upper = (^^person) -> person.name .= to-upper-case!
name-to-upper billy #=> 'BILLY'
billy #=> 'Billy'
@josher19
Copy link
Collaborator

@josher19 josher19 commented Sep 4, 2012

Work-around is:

name-to-upper = (person) -> person=^^person; person.name .= to-upper-case!
# or, in this case:
name-to-upper = (person) -> ^^person.name .= to-upper-case!

How would it handle default arguments?
(person=^^defaultV) works: clone defaultV when person is not defined.

To add it to the grammar, would need to have the AST transform this:

% livescript--ast -e '(^^person=defaultV) -> person.name'
Block
  Fun
    Unary ^^
      Assign =
        Var person
        Var defaultV
    Block
      Chain
        Var person
        Index .
          Key name

into this:

Block
  Fun
    Assign =
      Var person
      Var defaultV
    Block
      Assign =
        Var person
        Unary ^^
          Var person
      Chain
        Var person
        Index .
          Key name

which becomes the javascript:

(function(person){
  person == null && (person = defaultV);
  person = clone$(person);
  return person.name;
});

Extended test:


defaultV = name: \Idol

billy = name: \Billy
name-to-upper = (^^person=defaultV) -> person.name .= to-upper-case!
name-to-upper billy #=> 'BILLY'
name-to-upper! #=> 'BILLY'
billy #=> 'Billy'

defaultV #=> 'Billy'
@apaleslimghost
Copy link
Contributor

@apaleslimghost apaleslimghost commented Sep 7, 2012

You can fudge it using a second parameter:

name-to-upper = (person, clone = ^^person)-> clone.name .= to-upper-case!
@gkz gkz closed this in 03d2d3b Dec 30, 2012
@vendethiel
Copy link
Contributor

@vendethiel vendethiel commented Dec 31, 2012

Maybe it'd be interesting to have them in other places ? [i for +i in b when i is it]

@dubiousdavid
Copy link
Author

@dubiousdavid dubiousdavid commented Dec 31, 2012

Is this feature documented on the site right now?

David

On Dec 31, 2012, at 3:57 PM, Nami-Doc notifications@github.com wrote:

Maybe it'd be interesting to have them in other places ? [i for +i in b when i is it]


Reply to this email directly or view it on GitHub.

@vendethiel
Copy link
Contributor

@vendethiel vendethiel commented Dec 31, 2012

The proposed syntax, yes.
The snippet I posted is only a suggestion

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

Successfully merging a pull request may close this issue.

None yet
5 participants