View previous topic :: View next topic |
Author |
Message |
bobef
Joined: 05 Jun 2005 Posts: 269
|
Posted: Sat Mar 08, 2008 1:44 am Post subject: Few notes |
|
|
Hi,
Code: | global x = 5, y =6; |
Is not valid syntax. Why? What if I have 10 variables? Writing 'global' ten times? This is not right.
-------------
'local' and 'global' are too long to write; Is there some way to alias them?
-------------
Code: | global y = [1,2,"3"];
global z = [1,2];
y.length=z.length=5; |
This is not valid syntax (the last line). Why?!?
-------------
Code: | global y = [1,2,"3"];
for(local a=5;a<y.length;++a) writefln(a); |
It says "Attempting to access an array with a 'string'". What is that and why can't I write y.length which is natural for D and thus should be in MiniD?
-------------
, where 'a' is undefined produces some 'undefined symbol' (or something like that) error. Is it determined at compile time or run time? If at runtime an 'undefined' type (ECMAScript like) would be useful, so something like "if(a is undefined) {}" would be valid.
Thanks,
bobef |
|
Back to top |
|
|
JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
Posted: Sun Mar 09, 2008 2:52 pm Post subject: Re: Few notes |
|
|
bobef wrote: | Hi,
Code: | global x = 5, y =6; |
Is not valid syntax. Why? What if I have 10 variables? Writing 'global' ten times? This is not right. |
I'm considering adding it as an extension to the variable declaration syntax for MiniD2. I originally didn't have it because I felt the syntax would interact oddly with multivalued assingment:
Code: | local x, y = f(), z = 10 |
The tricky part is the semantics. Do we assign the results of f() into x and y, and then 10 into z? What if we want to declare x without initializing it and only having the first result of f() be put into y? Not to mention the declaration of z is sort of hidden in there.
It's a possibility for MiniD 2 but I wouldn't bank on it too heavily. Maybe if it were restricted to all single-valued assignments, like:
Code: | local x, y = (f()), z = 10 // x is null, y is the first result of f(), z is 10. |
That seems like a reasonable restriction.
Quote: | 'local' and 'global' are too long to write; Is there some way to alias them? |
You know I just don't agree. They're 5 and 6 letters apiece, and I can type them in a fraction of a second. You've mentioned it before, but sorry, you'll just have to live with it.
Quote: | Code: | global y = [1,2,"3"];
global z = [1,2];
y.length=z.length=5; |
This is not valid syntax (the last line). Why?!? |
Firstly MiniD doesn't support properties. Something like "a.b" already has a well-defined behavior that doesn't involve calling any functions (although that's changed in MiniD 2). It could instead (in MiniD1) be written as y.length(#z.length(5)). Yeah, whatever, if you don't like the syntax, I'm not that torn up.
Secondly, chaining assignment again interacts oddly with multiple assignment. It could be special-cased for single-valued assignment, but for how often the uses for chained assignment come up, I don't feel like it's justified. Furthermore, having assignments not yield a value prevents people from writing awful code like:
Code: | foo[something = f()] = bar[idx++] = 5; |
Is it my place as a language designer to prevent people from writing unreadable code? I'd like to think so. A language is meant to be read and used by humans after all.
Quote: | Code: | global y = [1,2,"3"];
for(local a=5;a<y.length;++a) writefln(a); |
It says "Attempting to access an array with a 'string'". What is that and why can't I write y.length which is natural for D and thus should be in MiniD? |
Because the name (which I even acknowledge is "admittedly ill-fitting" on the front page) does not mean that anything you can do in D, you can do in MiniD. The name was given back when the language really was a lot like D, sporting a static typing system which would make features like this easy and efficient to implement. I'd rather I'd have named it something else, but almost two years on, the name's kind of grown on me, even if it's not all that descriptive. After all, does Flowerscript have anything to do with growing plants? When it comes down to it though, I'm the one who decides what features are in the language or not, not D or Walter Bright. The name is not a binding contract.
As for this code snippet, again, MiniD does not have properties. And special-casing something like ".length" seems hackish to me. And for someone who thinks "global" and "local" are too long, I'm surprised that you don't prefer "#a" to "a.length"!
Looping through arrays can also be more tersely and efficiently written with a numeric for loop:
Code: | for(a: 0 .. #y) writefln(y[a]); |
Quote: | , where 'a' is undefined produces some 'undefined symbol' (or something like that) error. Is it determined at compile time or run time? If at runtime an 'undefined' type (ECMAScript like) would be useful, so something like "if(a is undefined) {}" would be valid. |
Local variable accesses (either in this function or in the enclosing functions) are bound at compile time. In the above code, if a local named 'a' is not found in this function or any enclosing functions, it is treated as a global and looked up at runtime. In MiniD 1, it is first looked up in 'this' (i.e. it's like doing "this.a"), and if not found there, it's looked up in the function's environment namespace. If it gets to the root of the namespace hierarchy and the name is not found, it's an error. Something like an undefined() function would be possible, which would check in "this" and the environment chain. I'll create a ticket for it. |
|
Back to top |
|
|
the_darkside_986
Joined: 04 Apr 2008 Posts: 10
|
Posted: Tue Apr 22, 2008 7:10 pm Post subject: |
|
|
I think that the explicit requirement of global/local and semicolons is one strength of MiniD over most scripting languages. It's easier to debug compile errors than runtime errors IMHO.
But I would like something like PHP's isset() and unset() functions that can take a literal variable, not just its name as a string, and return a boolean. Unset would remove the variable from the namespace not just set it to null. Of course, I could write functions that could do somthing like isset("myVar", mynamespace[=_G]); but that doesn't feel the same as doing isset(myVar,mynamespace);
And what about a delete keyword that would essentially do the same thing as the unset()?
Not that I particularly like most of PHP, but I like the isset() function. But I guess that would make the compiler trickier now that I think about it. (Having to avoid directly accessing the variable, causing an exception if it doesn't exist before calling the isset method, etc.) |
|
Back to top |
|
|
JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
Posted: Wed Apr 23, 2008 7:30 am Post subject: |
|
|
Having a function like isset() or unset() take a literal variable is just not possible within the semantics of MiniD as it is. Either the language would have to gain reference parameters (a level of complexity I'm not prepared to add to the language), or it'd have to be a built in pseudo-function, and I don't want the language to become Perl.
Secondly, even if they could take literal variables, the only namespaces you can modify at runtime are non-local string-indexed namespaces anyway, so it'd be nothing but syntactic sugar for using a string literal. In fact I'd have to provide a facility to allow for string expressions to be used so you could build up the name of a variable to unset at runtime.
Lastly, in a language where the local namespaces are fixed at compile time unlike PHP, functions like isset() and unset() become much less useful, and I really can't justify putting something that's not all that useful into the core of the language.
FWIW assuming that you had a function findGlobal, which would return the namespace of where a global lived, unset() would be:
Code: | function unset(name)
removeKey(findGlobal(name), name) |
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|