Re: Question on shapes

2022-05-17 Thread JG via Digitalmars-d-learn
On Tuesday, 17 May 2022 at 00:10:55 UTC, Alain De Vos wrote: Let's say a shape is ,a circle with a radius ,or a square with a rectangular size. I want to pass shapes to functions, eg to draw them on the screen, draw(myshape) or myshape.draw(); But how do i implement best shapes ? You could al

Re: Question on shapes

2022-05-17 Thread Dom Disc via Digitalmars-d-learn
On Tuesday, 17 May 2022 at 09:30:12 UTC, forkit wrote: On Tuesday, 17 May 2022 at 04:37:58 UTC, Ali Çehreli wrote: In you OOP example, I am curious why you chose Shape to be an interface, rather than a base class. You can inherit from multiple interfaces, but only from one base class. So

Re: Question on shapes

2022-05-17 Thread Ali Çehreli via Digitalmars-d-learn
On 5/17/22 02:30, forkit wrote: > On Tuesday, 17 May 2022 at 04:37:58 UTC, Ali Çehreli wrote: >> > > In you OOP example, I am curious why you chose Shape to be an interface, > rather than a base class. I always have the same question. :) interface feels lighterweight, so it is an arbitrary decis

Re: Question on shapes

2022-05-17 Thread forkit via Digitalmars-d-learn
On Tuesday, 17 May 2022 at 04:37:58 UTC, Ali Çehreli wrote: In you OOP example, I am curious why you chose Shape to be an interface, rather than a base class.

Re: Question on shapes

2022-05-16 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 17 May 2022 at 05:08:30 UTC, matheus wrote: In D there would be a better way to do such thing? Nothing really specific to D, but for one or two properties, you might just add them as function parameters with default values: ```d void draw(float scale = 1.0f); ``` If you have

Re: Question on shapes

2022-05-16 Thread bauss via Digitalmars-d-learn
On Tuesday, 17 May 2022 at 00:10:55 UTC, Alain De Vos wrote: Let's say a shape is ,a circle with a radius ,or a square with a rectangular size. I want to pass shapes to functions, eg to draw them on the screen, draw(myshape) or myshape.draw(); But how do i implement best shapes ? In addition

Re: Question on shapes

2022-05-16 Thread Ali Çehreli via Digitalmars-d-learn
On 5/16/22 22:08, matheus wrote: > interface Shape { >void draw(); >void draw(float scale); > } Interfaces can have 'final' functions: interface Shape { void draw(float scale); final void draw() { draw(1); } } Obviously, for that to work, now Circle.draw() etc. required to r

Re: Question on shapes

2022-05-16 Thread matheus via Digitalmars-d-learn
On Tuesday, 17 May 2022 at 04:37:58 UTC, Ali Çehreli wrote: ... 2) If you want to have a shape hierarchy, then you can start by defining its interface and implement that interface by concrete shape types. Drawing is ordinarily handled by member functions: ... Hi Ali, I'm not the author but I

Re: Question on shapes

2022-05-16 Thread Ali Çehreli via Digitalmars-d-learn
On 5/16/22 17:10, Alain De Vos wrote: Let's say a shape is ,a circle with a radius ,or a square with a rectangular size. I want to pass shapes to functions, eg to draw them on the screen, draw(myshape) or myshape.draw(); But how do i implement best shapes ? There are many ways of achieving thi