towerofnix / tlnccuwagnf

The Language Nobody Could Come Up With A Good Name For
MIT License
18 stars 3 forks source link

Access value of object by key expression #37

Open towerofnix opened 8 years ago

towerofnix commented 8 years ago

Kinda important.

I'd like this:

o:k
o:k > v;

But the problem with that is the ambiguity when you have multiple :ks..

o:k1:k2

Is that "k2 of (k1 of o)" or is that "(k2 of k1) of o"?

Probably the easiest thing to do is make the syntax like this instead:

o:[k1]:[k2]
o:[k1:[k2]]

If we do that we don't even need the colon any more:

o[k1][k2]
o[k1[k2]]

Aaaaand now we're copying JavaScript again! :tada:

plastpappa commented 8 years ago

Eh, just copy JS. :package: Easiest way to do it.

109C commented 8 years ago

If we copy js, we could make the interpreter very small and lightweight!

eval("<tulun here>")
bates64 commented 8 years ago

If people want to do the JS expression foo[bar[baz]], then they should do bap => bar:baz; foo:bap in Tulun. We don't want to copy JavaScript yet again...

towerofnix commented 8 years ago

The thing is, o[k1[k2]] makes more sense.. plus it doesn't need an extra variable. Which is better?

o[k1[k2]]
# or #
k_ => k1:k2;
o[k_];

Suppose I want to access o[k1[k2]] in an expression? I can't define variables in an expression! I'd need to do something like this:

print({ k_ => k1:k2; return(o:k_); }()); # inline function creation then evaluation #

Does that really seem any better than this..?

print(o[k1[k2]]);
bates64 commented 8 years ago

Hm... ooh, maybe offer both styles? We may as well :package:

towerofnix commented 8 years ago

Hm... ooh, maybe offer both styles? We may as well :package:

I'm only really interested in including good features, and I don't see how you can consider this good.. :P

print({ k_ => k1:k2; return(o:k_); }());
bates64 commented 8 years ago

I dunno, it's just that copying js seems like cheating

towerofnix commented 8 years ago

Perhaps looking at how other languages work with object-access-by-keys might inspire us?


EDIT:

Python and JavaScript do it like so:

obj_or_equivalent[key]

Ruby looks to be pretty much the same:

puts "#{hash[key]}"
bates64 commented 8 years ago

...well then. I guess obj[key] it is :|

We should probably make the syntax less javascripty, though.

towerofnix commented 8 years ago

Racket and Java each have their own functions, e.g.

my-hash-map => make-hash-map()
my-hash-map.set('bar', 'baz')
my-hash-map.get('bar')
towerofnix commented 8 years ago

We should probably make the syntax less javascripty, tho

+1 this. Any ideas on how to make it less JS-y? So far the best I think I've done is #34 :P

bates64 commented 8 years ago

How about this?

foo => obj();
foo.bar -> obj(
  'hi', 'Hello there!' # this needs to be implemented soon! #
);
foo:'baz': -> 'bar';
foo:('b' concat 'ar'):.hi # Hello there! #

# : is equivalent to [ and ], basically #