Recent

Author Topic: Limit variable scope  (Read 13656 times)

Kays

  • Hero Member
  • *****
  • Posts: 574
  • Whasup!?
    • KaiBurghardt.de
Re: Limit variable scope
« Reply #15 on: September 15, 2015, 03:57:38 pm »
Thx for all the replies. I guess the quintessence is there ain't such a way as i wished to limit var scopes. That design decision's [also] found in the philosophy of Pascal: Don't do stupid things. That philosophy's why I sticked to Pascal. So I'm OK with it, and I really don't need such a language construct but was just wondering whether it's possible.
Yours Sincerely
Kai Burghardt

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: Limit variable scope
« Reply #16 on: September 15, 2015, 06:23:26 pm »

And you are still not differentiating between eye candy and generated code. The C code with msvc and gcc is similar to that from fpc. I wish you properly checked that first.

How does the generated code matter? An untyped class, pointer and typed pointer are all the same in the generated code and still get their own type in a variable declaration.

It is not eye candy, it is error checking.

JavaScript's scoping would be eye candy.

I don't understand these topics. 

What is there to understand?

The current scoping is a cause of errors which could easily be fixed. It is like being forced to use a while loop, where you could use for-loops.

If you want C/C++/C#/Java, then use those languages.

C does not have generics. That makes it useless. (Pascal is useless too, till it gets a generics library.) C++ compiles too slowly. C#/Java are not natively compiled and thus have too much overhead.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11444
  • FPC developer.
Re: Limit variable scope
« Reply #17 on: September 15, 2015, 09:51:54 pm »
A language without block-scope variables is almost as primitive    as a language that only has (thread) global variables.

I always took block-scope variables as a 8-bit era leftover, same as alloca. Sure, you can make some exotic case for nearly everything, but in reality it is just baggage.

And free pascal has enough legacy of its own, we don't need to pile on C's.

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: Limit variable scope
« Reply #18 on: September 16, 2015, 02:11:53 pm »


I have a counter example in the C family:

- D is C/Java/C# syntax-like.
- D have great templates, generics, metaprogramming.
- D compiles fast (with DMD), no pre-processor. It's true unless you do some complex CTFE/metaprogramming stuff.
- D is natively compiled.
- D has a generic library (the 'range' concept: algorithms work with arrays, list, custom container, etc...).

http://dlang.org/

I thought about that. But last time I checked it had to incompatible standard libraries. Although that was years ago.




I always took block-scope variables as a 8-bit era leftover, same as alloca. Sure, you can make some exotic case for nearly everything, but in reality it is just baggage.
 
And free pascal has enough legacy of its own, we don't need to pile on C's.

I see it in the opposite way.

The proper block-scoping is a new feature.

It was the old C languages that requires one to declare all variables at the top of a block, before using them. Almost like Pascal

Only since C99 it works like I want it too


Thaddy

  • Hero Member
  • *****
  • Posts: 14358
  • Sensorship about opinions does not belong here.
Re: Limit variable scope
« Reply #19 on: September 16, 2015, 02:59:10 pm »

And you are still not differentiating between eye candy and generated code. The C code with msvc and gcc is similar to that from fpc. I wish you properly checked that first.

How does the generated code matter? An untyped class, pointer and typed pointer are all the same in the generated code and still get their own type in a variable declaration.

Well, not to put to fine a point to it, but I hope it hurts, because this is more like trolling:

I showed you already how to do proper block scoping by deferring to an inlined procedure or function.
Syntax wise it covers all the needs you so desperately want.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: Limit variable scope
« Reply #20 on: September 16, 2015, 04:09:05 pm »


I showed you already how to do proper block scoping by deferring to an inlined procedure or function.
Syntax wise it covers all the needs you so desperately want.

In your example, it is in the wrong order.
The function comes before the writeln, but is executed afterwards.
That is completely unreadable

I guess, you could put every line in its own function, but that will also be unreadable

del

  • Sr. Member
  • ****
  • Posts: 258
Re: Limit variable scope
« Reply #21 on: December 02, 2018, 06:31:31 pm »
I know this is an old thread but it showed up first in a Google search. FWIW my background is mostly C and Delphi / Lazarus. I got pushed into the C++ world when I had to use OpenCV. At first the memory management in C++ seemed very alien and chaotic and shocking.

Now that I'm starting to get it a little bit I think having temporary objects go out of scope (and get "released" automatically) at the end of each loop iteration is very cool IMHO. It's not just eye candy. Ironically - the more I learn about C++ the more I want to learn about Object Pascal (even though I've been using it for years).  ::)

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Limit variable scope
« Reply #22 on: December 02, 2018, 10:02:10 pm »
I think having temporary objects go out of scope (and get "released" automatically) at the end of each loop iteration is very cool IMHO.

I hope you are aware in C/C++ this works only for a (let's call it) "simple variables", i.e. stack allocated. You still need to call destructor manually on heap-allocated (by poiners and new, like Pascal's TObject) objects.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

 

TinyPortal © 2005-2018