PDA

View Full Version : Naming - Creating objects.



marmin
19-05-2007, 11:34 AM
var
Tpar = class
// data
// constructor, destructor, etc.
end;


procedure test;
begin

par_001 := tpar.Create;
foo_x := tpar.Create;
nice_weather := tpar.Create;


end;


Why isn't this implemented, it's much easier.
Why do I have to tell the compiler what names I will use in advance. It strikes me as being a bit.. stupid. Why isn't compiler smart enough to recgnize a creation of an object and do the rest.


Now, I am forced to use only new () and Dispose () (the old fashioned way), because I don't want the compiler to know what instances i'm going to use.

Robert Kosek
19-05-2007, 12:14 PM
Why isn't this implemented, it's much easier.This what? Maybe if you tell what you are puzzling over we might be able to answer questions. As far as I can tell from your snippet ... it is implemented.



Why do I have to tell the compiler what names I will use in advance. It strikes me as being a bit.. stupid. Why isn't compiler smart enough to recgnize a creation of an object and do the rest.That's because they designed the compiler around an explicit declaration of constructors and deconstructors, not the other way around. So? It's just like C# and Java's implementation of Namespaces ... whoopie, a feature.

Don't forget that even a bug can be considered a feature.


Now, I am forced to use only new () and Dispose () (the old fashioned way), because I don't want the compiler to know what instances i'm going to use.Yeah, because you aren't following what was set as the standard by your compiler. And whose fault is that choice? Obviously not the compiler when it is a quite limited program, though advanced, when compared against a human.

marmin
19-05-2007, 01:14 PM
This what? Maybe if you tell what you are puzzling over we might be able to answer questions. As far as I can tell from your snippet ... it is implemented.

>No, afaik you first need to make



type
par_001,
foo_x,
nice_weather : Tpar;
:( and that's what i'm talking about if you don't do it it gives an error.





Why do I have to tell the compiler what names I will use in advance. It strikes me as being a bit.. stupid. Why isn't compiler smart enough to recgnize a creation of an object and do the rest.That's because they designed the compiler around an explicit declaration of constructors and deconstructors, not the other way around. So? It's just like C# and Java's implementation of Namespaces ... whoopie, a feature.

>Then why not make it better, it is a burden to do, and, it is a waste of time. A bad feature needs to be removed.


Now, I am forced to use only new () and Dispose () (the old fashioned way), because I don't want the compiler to know what instances i'm going to use.Yeah, because you aren't following what was set as the standard by your compiler. And whose fault is that choice? Obviously not the compiler when it is a quite limited program, though advanced, when compared against a human.[/quote]

>in c++ you can just do it . That's not my fault..

Setharian
19-05-2007, 01:15 PM
now I do not understand what you actually mean....

marmin
19-05-2007, 01:24 PM
Well, Setharian, it's simple, in my first post if you execute it you'll get:

'undeclared identifier : par_001'.
And that is my point. If the compiler can't figure out that it belongs to tpar, and automatically make a type for that, then I see that as a flaw.

Setharian
19-05-2007, 01:31 PM
if you mean explicitly declaring variables then you'll have to live with that....it has been in Pascal from the very start and makes you think about variables before you start using them....if you can't get used to it, stay with C(++)....

marmin
19-05-2007, 01:36 PM
That 's the general reaction. If you propose change in any way, you are politely asked to go away from Pascal, like if I 'd be some kind of 'enemy' towards the language and gonna steal the Golden crown away from it. :D

There is almost no in-depth discussion, as to the question if there really need to be changes like that. I don't think I'd go to a stronger language for now.. the C++ scene is too crowded. but it's more and more tempting.. :D

jasonf
19-05-2007, 01:46 PM
Pascal doesn't do declairation on the fly. That's by design. Some hate it, some love it. We're not coding VB here, this is object pascal.. a much better language by far..

Anyway about your question.. this code will work




Tpar = class
// data
// constructor, destructor, etc.
end;


procedure test;
var
par_001, foo_x, nice_weather : tpar;
begin

par_001 := tpar.Create;
foo_x := tpar.Create;
nice_weather := tpar.Create;


end;

It's the way the language works. The compiler doesn't attempt to assign the variable to the type used for the constructor unless it's told to, it would only add a level of complexity and possibly introduce unexpected behaviour.

Hope this helps.

http://www.delphibasics.co.uk/RTL.asp?Name=Var

Setharian
19-05-2007, 01:48 PM
the obvious problem is that people expect all languages to have their favourite features....if they encounter something which is no the same, they ask why is it so and if it can be changed....well the simple answer is it's a different language and no it won't get changed.....pascal and basicly all algol languages are completly different from C-based languages (C, C++, D, Java, C# = all go here)...it's a different family of languages and thus it has different coding conventions and syntactic rules....

marmin
19-05-2007, 02:13 PM
....if they encounter something which is no the same, they ask why is it so and if it can be changed. I didn't compare it to any other language in my first post. If you fail to see the advantages of my proposal, I can't help it.

Setharian
19-05-2007, 02:24 PM
no, but you did in your second one :) and I replied after it ;)

JSoftware
19-05-2007, 04:48 PM
If you fail to see the advantages of my proposal, I can't help it.

I code in C as one of my jobs. I praise, and would at any time choose, the pascal way of declaring variables. Having a section for itself for variables is just so much better for clarity and coding efficiency. Especially experienced c coders utilize extremely ugly and confusing variable declaration practices.

That's where I like the way pascal requires a certain way of variable decleration for everyone. I see no advantage of your proposal

Mirage
21-05-2007, 04:24 AM
Why do I have to tell the compiler what names I will use in advance. It strikes me as being a bit.. stupid. Why isn't compiler smart enough to recgnize a creation of an object and do the rest.

You have to tell the compiler what variables of which type you will use to allow the compiler to perform type checking and find 90% of stupid errors ("undeclared identifier", "type mismatch" etc) at the compilation stage.
If you are a computer and never make errors this is not for you.


Now, I am forced to use only new () and Dispose () (the old fashioned way), because I don't want the compiler to know what instances i'm going to use.
>in c++ you can just do it . That's not my fault..

In C++ you still have to declare all variables.
In Object Pascal you can use metaclasses to decide instance of which class to create at runtime.
And C++ can not do like this.

Sly
21-05-2007, 05:11 AM
To answer the original post, how often do you make typos in variable names? If you answer "never", you are fooling yourself. Many script languages use the "create a variable first time it is used" method that you want, but the main problem with that is if you make a typo in a variable name, it creates a new and different variable than the one you intended. Then you spend the next hour or more wondering just WTF is going on with your code.



If you fail to see the advantages of my proposal, I can't help it.

I code in C as one of my jobs. I praise, and would at any time choose, the pascal way of declaring variables. Having a section for itself for variables is just so much better for clarity and coding efficiency. Especially experienced c coders utilize extremely ugly and confusing variable declaration practices.

That's where I like the way pascal requires a certain way of variable decleration for everyone. I see no advantage of your proposal

I use C++ in my usual job, and I adore how variables can be declared just prior to use. It annoys me in Pascal to have to move up to the top of the function just to declare a variable, then move down again to where I want to use it. Also, being able to re-use the same variable name for several different things is great for reducing the number of esoteric variable names that get created just to differentiate one variable from another.

This is a very simplistic example, but it shows how we use variable declarations.


for &#40;int i = 0; i < 4; ++i&#41;
&#123;
NodeType1 *pNode;
// Do something with i and pNode
&#125;

for &#40;int j = 0; j < 4; ++j&#41;
&#123;
NodeType2 *pNode;
// Do something with j and pNode
&#125;

JSoftware
21-05-2007, 07:01 AM
I think this is becomming a flame war

I use C for microprocessors, which might explain something. Here you have to think about any variable you declare as the ressources I have are extremely sparse. Having the variables declared in a section for itself would be way better for me.

I can ofcourse do that but others can choose not to do that and use inline decleration of variables. Then the day I need to weed out in the loads of variables declared, resize too big arrays, etc. I'll have to sit and look through every line in each procedure to look for some very camoflaged variables

That's such a PITA

savage
21-05-2007, 08:57 AM
Marmin, if you don't want to declare a variable you don't have to, you could always use the with keyword :).


with TMyClass.Create do
begin
// call all the class methods here.
end;


Of course there is nothing stopping you from creating your own branch of Object Pascal that implement the features you describe.

Computer languages are very Darwinian in nature. Those that people find useful survive, and those that are not seen as useful will not survive. So my suggestion is to create your own compiler and let it loose on the community. If developers use it and find that it meets their needs, it will survive.

Dan
21-05-2007, 09:09 AM
Computer languages are very Darwinian in nature. Those that people find useful survive, and those that are not seen as useful will not survive.
Then how come Visual Basic (and other branches of basic language) still exist? :wink: They must have met a dead end of evolution decades ago. :P

savage
21-05-2007, 10:16 AM
Then how come Visual Basic (and other branches of basic language) still exist? :wink: They must have met a dead end of evolution decades ago. :P

Whether we like it or not VB fitted a lot of developer needs in it's day. I actually believe that without the proliferation of VB in the Win 3.1 and early Win32 days, the Windows market would not have grown as quickly as it did. It allowed the creation of lots of new software rapidly. More software sold more Operating systems etc. So it had it's place. But VB is no longer as popular as it was and I think is reaching it's evolutionary end.