Nick Hodges

Random Thoughts on the Passing Scene #86

15 Responses to “Random Thoughts on the Passing Scene #86”

  1. 1
    Rick Says:

    I read Joe Stagner’s Delphi for PHP and noticed the following quote: "They have new 2009 versions of Delphi for .NET and Delphi for Windows as well."

    What is the ETA for Delphi .NET roadmap?

    Thanks,
    Rick.

  2. 2
    Xepol Says:

    Generics and Anonymous methods are some of those things that really aren’t impressive in an of themselves. However, they can form the foundation of some truely impressive code libaries.

    Soon, type inference, nullable types, and then maybe on to compiler magic to include linq syntax?

    Var S;

    Begin
    S := TStringList.Create;
    Try

    Finally
    FreeAndNil(S);
    End;
    End;

    The compiler isn’t stupid after all and since S doesn’t matter until its created, a boatload of typing could be saved through out the delphi universe.

  3. 3
    Luigi D. Sandon Says:

    Xepol: I prefer stable and error free code than "saving some typing". What happens if you make a mistake and write S := ‘foo’ somewhere after S := TStringList.Create? Actually it throws a compiler error.
    Pascal has its rules - for good reasons, and I will be very careful about making it too much alike other languages born to implement different kind of applications, just to "save typing".

  4. 4
    Lars Fosdal Says:

    Why the "var S" at all ? Most local variable types can be derived from the context.
    The type of S is given from the class.create. A for-loop index variable type can be derived from the for-loop enumerable constraints. Booleans can be derived from the expression context.

    If anything declarative is needed, I would have preferred this:

    begin
    var S := TStringList.Create;

    Actually, I would prefer some sort of scope delimiting of the local variable and definitive rules forbidding re-typing outside subclass scope (ie you can’t first use a var S (as integer) then later a var S (as string) in the same block.

  5. 5
    Xepol Says:

    @Luigi - I believe you have confused dynamic typing with Type inference. In type inference, once determined, the type does not change again. Type inference does not weaken strong typing, but rather asks the question : why do I keep declaring a variable type multiple times just to assign a value into it?

    @Lars - So the compiler has some clue that you mean the type to by infered and that you did not miss type "s" instead of "d" or "S2". Besides which, there is always a matter of scope. If you don’t declare the scope for a variable, you may not be happy with where the compiler thinks it should go. However, your inline var works for me. I have frequently wished Delphi would adopt it from the C family.

  6. 6
    Alexandre Machado Says:

    @Lars
    In fact I generally write this code:

    var S: TStrings;
    begin
    S := TStringList.Create;

    I’m declaring S as TStrings, but creating it as a TStringList, a TStrings descendant. That’s what OOP is about, isn’t it? Or I could do something like that:

    var Obj: TMyPesistentClass;
    begin
    Obj := TMyPersistentClass(GetClass(’MyClassName’)).Create;

    That construction: var S := TStringList.Create;
    will not handle such cases in the same way.

    Regards

  7. 7
    Xepol Says:

    @Alexandre -> I have seen that usage a number of times and find it to be very, very limiting. You only get the functionalty exposed by the ancestor.

    Honestly, I don’t actually ever use TStringList any more. I use a custom descendant with extra functionality, that I use in almost every case.

    Things like :
    .Add(FormatStr : String; Const Args : Array Of Const); (ditto on insert)
    .SeperatedText[Seperator : String] (.Text with the cr/lf translated into anything I want both in and out)
    .RemoveBlankLines(RemovalOptions : TRemovalOptions); ( Leading, Contained, following blank lines)
    .IndexOfValue(aValue : String) : Integer;

    and a huge pile of other functionality, including the ability to sort the strings in a more human friendly way so that the result doesn’t look like: Line1, Line10, Line2 but rather : Line1, Line2, Line10

    If you ever get into generics, again, using a base class as a reference holder will pretty much defeat the point of using generics.

    There is one place that I do use the base class and that is to make it flexible to pass IN a reference to an object.

    So something like : LoadComboBoxFromStrings(CB : TComboBox; S : TStrings);

    Oh, and your second example does work, and TCollection is based on similar code. That said, unless you are using it in a component, chances are that a generic with exact typing will work better in future.

    As far as the argument that var s:= TStringList.Create will not handle things in the way you are used to, it isn’t an argument - it is the point. It doesn’t replace the current method but rather extends it, so the ability to do things the way you have classically done them is still there while the opportunities that infered typing allows would then also exist.

  8. 8
    Alexandre Machado Says:

    @Xepol:
    The code using TStrings/TStringList was just an example that there are times that the compiler doesn’t know the actual class that I will use. So… The "var s:= TSomeClass.Create" construct is not able to handle all situations.
    If less typing is your main concern, there is a refactoring "SHITF+CTRL+V" that will declare S as TStringList for you, and save you a lot of typing ;)
    CodeGear should focus on bring new value to their customers, evolving VCL, database access layers, etc. rather than creating cosmetic changes to the language itself, IMO.

    Best Regards

  9. 9
    Jolyon Smith Says:

    @Xepol

    "The compiler isn’t stupid after all"

    Indeed not, but programmers often make stupid mistakes.

    ime - the clearer you have to be in your code the more likely you are to catch *yourself* making a dumb mistake.

  10. 10
    Lars Fosdal Says:

    how about this form for overriding the declaration type of S?

    begin
    var S := TMyStringList.Create as TStringList;

    end;

    S is not available past the end of the block?

  11. 11
    David Heffernan Says:

    We seem to be talking about type inference here and a lot of posters seem to be scared of it.

    For what it’s worth I’d love type inference just like MS recently introduced to C#. In other words you could be able to write:

    begin
    var S := TMyStringList.Create;
    for var i := 0 to S.Count-1 do

    end;

    Personally I’d be happy to see the majority of Delphi development going to language/compiler enhancements. I’m happy with D6 IDE and database doesn’t mean much to me. My product is a numerical FE package with a powerful GUI so Delphi is perfect.

    Obviously I realise that other Delphi users have different needs to I totally accept that there will be a lot of developments that target users other than me. Just so long as language/compiler developments don’t get forgotten and in recent times there has been good progress there: operator overloading, generics, closures etc.

    Now the real icing on the cake would be the cross-compiling 64 bit compiler. I guarantee I would buy that!

  12. 12
    Luigi D. Sandon Says:

    Compiler/language improvements should be driven by other reasons than "less typing".
    Type inference was introduced in C# 3.0 to support features borrowed from functional programming (especially to support LINQ, I guess, anonymous types looks to be introduced for the same need).
    Type inference can’t simple use the first declaration and use it, or it would be a great stopper of inheritance - IIRC there are algorithms to choose the most general type to be used, yet introducing new code the type can change.
    IMHO, C# is chasing more dynamic languages like Ruby, because they cover the same market - web applications - most of C# development is targeted to.
    Adding new features to Delphi should be a different approach to the "me too" one. Would such a feature improve applications, beyoond "less typing"?

  13. 13
    Alan Fletcher Says:

    "me too" and "less typing" are not language features worth pursuing. If I want C# features I will use C#! (OMG! I can’t believe I typed that!).

    Besides those kind of features tend to bastardize the language.

    I agree with some of you that are set on better IDE, 64 bit support, cross compiler, etc… Deliver that along with bleeding edge innovation and I will go out of my way to use Delphi (not that I don’t do that today :P)

  14. 14
    Bob Pointon Says:

    I am not to bothered by less typing as I find the more typing the easer the code is to read in eight years time. But a good way to save typing is to have an intelligent editor. if we had a programmable editor a la Brief a lot of the declaration and instantiation could be done by the editor.

  15. 15
    Nick Hodges Says:

    Bob –

    Have you taken a look at what you can do with Live Templates?

    Nick

Leave a Reply

© 2008 Nick Hodges | Entries (RSS) and Comments (RSS)

Your Index Web Directorywordpress logo
Close