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

about Semiautovivification and f# cloning record implementation #53

Closed
cocodrino opened this issue Jun 21, 2012 · 12 comments
Closed

about Semiautovivification and f# cloning record implementation #53

cocodrino opened this issue Jun 21, 2012 · 12 comments

Comments

@cocodrino
Copy link

Hi!!...first I wanna comment about the weird syntax for "Semiautovivification" (it's the first time than I heard that word :D)

x.@@'hello world'[email protected] 

I'm not sure cause the example wasn't totally clear for me...but is the same syntax for th coffeescript code

zip = lottery.drawWinner?().address?.zipcode  ??

I feel it a bit complicate and weird compares with the coffeescript...the livescript syntax here offer any benefit?

the second point is abot cloning record..would be nice has cloning record in livescript

personA = {name : "matias" , age : 20  , job : "a cool job", ..........}
{personA with name='jhon'}   //syntax example...copy personA changing the name

UpperCaseName => (person) ->
    {person with name=person.name.toUpperCase()}

I don't know..but for me...this looks cool :D.....
maybe you know other cool way..please share your opinion.....

@gkz
Copy link
Owner

gkz commented Jun 21, 2012

You can use

lottery.drawWinner?().address?.zipcode 
# or for shorter:
lottery.drawWinner?!address?zipcode 

in LiveScript. Semiautovivification is different, unlike the above if a property doesn't exist as an object or array it will make an empty one.

As for the cloning with changing, you can do

personA = 
  name : "matias" 
  age : 20
  job : "a cool job"

personB = ^^personA <<< name: \john

Note that the JSON output you will see for personB on the site repl will only show the name, because JSON.stringiy does not show properties of the prototype, but personB does in fact have an age and a job, which you can access.

@gkz gkz closed this as completed Jun 21, 2012
@adrusi
Copy link

adrusi commented Jun 22, 2012

how about adding an alias for for ^^ ... <<< ... ... with .... I don't think that infix with would be ambiguous with

with person
  @job = "ditch digger"

then you could do:

person with hair: black

this is actually very beautiful javascript, I much prefer it to trying to imitating classes

@cocodrino
Copy link
Author

I think than any feature than become more readable (even for person unfamiliar with the language) is a nice inclusion...."with" let a very readable code and understable for any people...

@adrusi
Copy link

adrusi commented Jun 27, 2012

@gkz would you accept a pull request implementing this above proposes infix with?

@gkz
Copy link
Owner

gkz commented Jun 27, 2012

Yes, as long as it doesn't break any existing code.

@satyr
Copy link
Contributor

satyr commented Jun 27, 2012

The clone operator used to be postfix {} which could define initial properties altogether until object slice got in.

person with hair: black

Implying clone with with seems confusing since the current with is itself useful with the clone operator:

with person2 = ^person
  @hair = black
  ...

which corresponds well to this idiomatic Io code:

person2 := person clone do(
  hair := black
  # ...
)

gkz added a commit that referenced this issue Jun 27, 2012
personA = name: \John, age: 20
personB = personA with name: \Amy
personA.name == \John
personB.name == \Amy
personB.age  == 20
@gkz
Copy link
Owner

gkz commented Jun 27, 2012

Well screw it I was bored so I added it.

personA = 
  name: \matias
  age:  20
  job:  'a cool job'

personB = personA with name: \john

eq \john   personB.name
eq \matias personA.name

personC = personA with
  name: \amy
  age:   19

eq \amy    personC.name
eq 19      personC.age
eq \matias personA.name
eq 20      personA.age

The only bad side affect is that you can't use a with statement as an argument for something, but that seems rare so I thought it was okay.

If we decide this is stupid I'll revert.

@gkz gkz reopened this Jun 27, 2012
@adrusi
Copy link

adrusi commented Jun 27, 2012

well that's okay I guess. I started working on it, got the tokens->AST->JS process done without a hitch, but that's the easy part I guess because the grammar file and I were not making very good friends.

Interestingly I was writing this comment and received a notification of your reply on the google group right before hitting "comment". I'll have to look at how you went about this so that I can contribute more effectively in the future.

@gkz
Copy link
Owner

gkz commented Jun 27, 2012

I'd be interested in seeing what you did, please post here with your work.

edit:
I just saw your commit on your fork of LiveScript, looking at that now.

@vendethiel
Copy link
Contributor

@gkz Maybe we can use do here too ?

base-person =
  name: \Person
  job: \worker

old-person = base-person with age: 80
old-garry = old-person with do
  name: \Garry
  job: null

Or maybe this would be better in the other construct ?

with old-person do
  @name = \Oldman

@adrusi
Copy link

adrusi commented Jun 27, 2012

@gkz oh noes, now you see my code that looks so long (and non-functional) relative to your 3-4 line solution.

Also, what about partially applied infix with. I don't see the point, it's not something you'd do in normal prototypal inheritance, but I feel like someone will complain that it doesn't work like other infix operators. I say just leave it the way it is.

@Nami-Doc I haven't pulled the new compiler yet, so I haven't tested it, but I'd assume that

old-garry = old-person with do
  name: \Garry
  job: null

would already be valid, and therefore be inconsistent to make it do something else.

In any case, the old with statement is an imperative construct so do makes more sense there.

@gkz
Copy link
Owner

gkz commented Jun 27, 2012

@Nami-Doc You can already add new properties, and use it with blocks (without do).

@gkz gkz closed this as completed Jun 27, 2012
@gkz gkz reopened this Jun 27, 2012
@gkz gkz closed this as completed in b2ff1b1 Jun 27, 2012
This was referenced Jan 11, 2013
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

5 participants