Recent

Author Topic: Generic primitives, generic records.  (Read 19311 times)

Wodzu

  • Full Member
  • ***
  • Posts: 171
Generic primitives, generic records.
« on: February 25, 2010, 07:02:43 pm »
Hi, is it possible to create something like this:

Code: [Select]
type
  T3DPosition = record
    X: _T;  //instead of T any primitive type f.e. Single, Double, Integer...
    Y: _T;
    Z: _T;
  end;

var
  Pos1, Pos2: T3DPosition;

begin
  Pos1.X := Pos1.X + Pos2.X; //X is any single primitive
end;

Do you catch my drift? ;)

azagaros

  • Newbie
  • Posts: 3
Re: Generic primitives, generic records.
« Reply #1 on: February 25, 2010, 10:48:21 pm »
My suggestion is learn Object Orientated programming.  You can do this with OOP tricks of Free Pascal.  I don't remember the exact code.. I can do this in C++ easily, another OOP language, even with overloaded operators and Run time information.

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Generic primitives, generic records.
« Reply #2 on: February 25, 2010, 11:15:41 pm »
This is not an answer for my question.

But thanks anyway, I've never laugh so much reading "technical" post.  :)
Especially the part with C++ (totaly irrevelant  :o) and RTTI (even if FPC would publish it for records(!) that would be so ineffective).

So in other words, everything is easy but you don't remember the exact code :)

Are you a forum troll or something?

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1216
    • Burdjia
Re: Generic primitives, generic records.
« Reply #3 on: February 26, 2010, 09:50:33 am »
Wodzu: I don't understand. Your code is a valid record and will compile without problems (if _T was previously defined ;) ) so, what are you actually asking for?
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Batuhan

  • New Member
  • *
  • Posts: 14
Re: Generic primitives, generic records.
« Reply #4 on: February 26, 2010, 10:35:58 am »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Generic primitives, generic records.
« Reply #5 on: February 26, 2010, 10:36:25 am »
Perhaps he'd like to treat _T as a template. You can use generics for this, ex:
Code: [Select]
type
  generic T3DPosition<_T> = class
    X: _T;  //instead of T any primitive type f.e. Single, Double, Integer...
    Y: _T;
    Z: _T;
  end;
 
  TInteger3DPos = specialize T3DPosition<Integer>;

var
  Pos1, Pos2: TInteger3DPos;

begin
  Pos1:=TInteger3DPos.Create;
  Pos2:=TInteger3DPos.Create;
  ...
  Pos1.X := Pos1.X + Pos2.X; //X is any single primitive
  ...
end;
Currently generics have problem when the + operator is not overloaded before the generic class definition. So, if you're going to use non-primitive types declare the operator definition before the generic class.

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Generic primitives, generic records.
« Reply #6 on: February 26, 2010, 10:56:50 am »
Thank you guys for your answers:

@Ñuño_Martínez : I want to create a template for X,Y,Z and I want to store it in record. The template should be able to "support" any simple type: Integer, Double, Single, Byte and so on.

@Batuhan : Thanks but it will be to slow. I need to perform large number of calculations during runtime.

@Leledumbo : Close, but I don't want to use a class. It is to much overhead to keep tree simple variables.

Records are yet not supported?  :(

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1216
    • Burdjia
Re: Generic primitives, generic records.
« Reply #7 on: February 26, 2010, 12:22:32 pm »
Records are yet not supported?  :(
  :o You're kidding, aren't you?

Records are supported. They're a basic feature of Pascal language.

(...) Your code is a valid record and will compile without problems (if _T was previously defined ;) ) (...)

As Leledumbo said it seems you're asking for generic classes* or may be interfaces.

(* Actually I think "generic classes" are a bad programming practice and shows poor design capabilities. Actually that's one reason I hate C++/C#/Java so much.)
« Last Edit: February 26, 2010, 12:27:00 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Generic primitives, generic records.
« Reply #8 on: February 26, 2010, 12:53:49 pm »
Ñuño_Martínez sorry, I meant generic records like in my topic title.
I've also said that I don't want to use a generic classes. I want generic records :)

I do not think that they are bad programming pracitce and show poor design capabilities.

I will give you one example, and you tell me what is wrong with this example.
Code: [Select]
function AddNumbers(Number1, Number2: Integer):Double; overload;
function AddNumbers(Number1, Number2: Single): Doube; overload;
function AddNumbers(Number1, Number2: Double): Double; overload;

To add two numbers you need to have 3 overloaded methods.

But with generics you could do it like this:
Code: [Select]
function AddNumbers(Number1, Number2: _T): _T; overload;

(Please note that this is only an example in pseudocode. I don't know how to define generics in Pascal yet)

What is wrong with that?

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1216
    • Burdjia
Re: Generic primitives, generic records.
« Reply #9 on: February 26, 2010, 01:54:20 pm »
Ñuño_Martínez sorry, I meant generic records like in my topic title.
Ok then not, "generic records" aren't supported AFAIK.

What is wrong with that?
Hard to explain in plain English (I'm European ;) ).

IMO "generics" opens a door to "anything". It's powerful but it's a common source of problems too. I have the same problem with operator overload. When I used C++ I had a lot of problems with all that stuff and when I read that FPC planned to support them I thought "Oh, no! Here we go again!". :(

I remember a nightmare-project (the last I used C++) where in some cases returned bad results. There were no errors in compilation nor in execution. Just a value changed with apparently no reason. We needed almost two days to discover that the problem was a combination of operator overloading and templates (as C++ calls "generics") in the deepest of the STL library. The compiler did a bad conversion somewhere before to call a bad assignation operator (overloaded) and finished with the creation of a bad implementation of a container.  We had to rewrite almost the whole thing including several of the STL classes and I (and a coworker) promised myself to never use templates nor operator overloading again in my life. Of course this wasn't the first time we have a similar problem but before this we were lucky and can fix it easily.

If we were do it in Delphi, we would use interfaces which solves the same problems in a more elegant way IMO. Of course nobody forces me to use "generics" at the moment and I can't forbid you to use them but I think you really don't need them at all.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Generic primitives, generic records.
« Reply #10 on: February 26, 2010, 03:30:45 pm »
I am sorry to hear that. But I think that in your case it was not error caused by compiler but by wrong usage of a tool (templates, operator overloading). Every tool might be used in a wrong way but that doesn't mean that the tool itself is wrong :)

For example. I can take a knife and cut the loaf of bread but accidently I can cut my finger too :) Does it mean that the knife is bad? ;)

Anyway, I have shown you an example in which I think that generics might be useful. I would like to ask you to show me in my particular (taken from life) example what is bad in using genrics here :)

raistware

  • New Member
  • *
  • Posts: 15
Re: Generic primitives, generic records.
« Reply #11 on: February 26, 2010, 04:07:56 pm »
I can, it generates a very difficult to debug code. I also hate templates, generics or whatever you want to name it.

Pascal is a heavy typed programming language, and IMO think that generics is not a good thing to use. For simple types can work good, but abuse of templates or generics converts code to something unmantainable to me.

But it is just my opinion.

raistware

  • New Member
  • *
  • Posts: 15
Re: Generic primitives, generic records.
« Reply #12 on: February 26, 2010, 04:10:44 pm »
Also, you should ask this on FPC forums / lists, not in Lazarus one ;-)

eny

  • Hero Member
  • *****
  • Posts: 1651
Re: Generic primitives, generic records.
« Reply #13 on: February 26, 2010, 04:14:06 pm »
Pascal is a heavy typed programming language, and IMO think that generics is not a good thing to use

Generics are a very powerful mechanism to make programs even more typesafe!
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Generic primitives, generic records.
« Reply #14 on: February 26, 2010, 04:16:53 pm »
For simple types can work good, but abuse of templates or generics converts code to something unmantainable to me.

So again we are talking about the wrong usage of a tool ;) And I was talking about simple types...

Also, you should ask this on FPC forums / lists, not in Lazarus one ;-)

Why not? This is Forum->Programming->General so we are talking here about programming in Pascal or am I wrong?:)

Unfortunaltey FPC mailing list is very hard to read :|

 

TinyPortal © 2005-2018