Feature Request: Object cloning in function parameters #161

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

Projects

None yet

5 participants

@dubiousdavid

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
Collaborator
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'
@quarterto
Contributor

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
Contributor

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

@dubiousdavid

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
Contributor

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