Re: switch off GC?
bearophile пишет: > Weed, some built-ins and significant part of the standard lib (and other libs > you can find around) assume the presence of a GC. Right. For example, return from function of a dynamic array will not work correctly without GC - there will be a memory leak. > So you can disable (or often just not use it. If you don't allocate/free > GC-managed memory then the GC sleeps) in some critical spots of your code as > I do (and you can also just use memory from the C heap), but if you really > want to remove it, you may want to consider using C++ instead. > > I'd like too to have something like a -nogc compiler flag to not include the > GC at all in small programs that don't use the GC (or use it only to allocate > memory and not to free it). My wishlist also contains -nogc (or may be -gc?) too. But it seems to me it will not occur - too many parts of language mean presence GC. I just would like that D could substitute C++ completely in all applications...
switch off GC?
It is possible to disable GC? That it has not been included in result binary for an increasing performance of ref operations and reduction of the size of the binary I have not found the answer in google.
swtch off GC?
It is possible to disable GC? That it has not been included in result binary for an increasing performance of pointers operation and reduction of the sizes of the binary I have not found the answer in google.
Re: trouble with dmd + obj2asm under linux
Daniel Keep пишет: > > > Weed wrote: >> [snip] > > If I had to guess, I'd say it's because you compiled without symbols. > Try this: > > $ dmd -g demo.d Has varied nothing. Names of functions and were earlier in a binary file, they are enumerated from above (I have reduced them): public _D2rt8typeinfo7ti_uint10TypeInfo_k5tsizeMFZk public _d_isbaseof2 public _D6object14TypeInfo_Const5flagsMFZk extrn pthread_key_create@@GLIBC_2.0 public _D4core6thread5Fiber9freeStackMFZv public _D6object14TypeInfo_Tuple7getHashMFxPvZk public _D2rt4deh29FuncTable6__initZ .init segment assume CS:.init pushEBP mov EBP,ESP However, objdump works quite normally for D a code
trouble with dmd + obj2asm under linux
I wrote and compile program: $ dmd demo.d And I need disassembly it: $ obj2asm ./demo ./demo.d -cdemo.cod And I see asm file "demo.cod" without names of functions: == .init segment assume CS:.init pushEBP mov EBP,ESP pushEBX sub ESP,4 callnear ptr LC LC: pop EBX add EBX,01056Ch mov EDX,-8[EBX] testEDX,EDX je L22 callnear ptr LC0 L22:callnear ptr L3F8 callnear ptr LD338 pop EAX pop EBX leave ret .init ends .pltsegment assume CS:.plt L0: pushdword ptr [080591A4h] jmp dword ptr [080591A8h] == >From other person I have received such listing with names: == assume CS:_D4test1C5opAddMFC4test1CZC4test1C L0: pushEAX pushEBX pushoffset FLAT:_D4test1C7__ClassZ callnear ptr __d_newclass mov EBX,EAX mov EAX,8[ESP] mov ECX,8[EAX] mov EDX,010h[ESP] == What it is necessary to make such listing? $ obj2asm Digital Mars .OBJ file disassembler Version 8.51.3 Copyright (C) Digital Mars 2000-2007. All Rights Reserved. Written by Walter Bright http://www.digitalmars.com/ctg/obj2asm.html Use: obj2asm [-l] [-o] objfile[.obj] [srcfile] [-c[outfile[.cod]]] -c Specify output filename -l Omit generated code labels -o Output object code for each assembly instruction. -x Emit code offsets
Re: compile time method check
Bill Baxter пишет: > 2008/12/27 Weed : >> Simen Kjaeraas пишет: >>> On Fri, 26 Dec 2008 20:15:30 +0100, Weed wrote: >>> >>>> Can I at compile time check whether there is a facility method >>>> (toString(), for example)? >>>> >>>> Today I wrote: >>>> >>>> static if ( __traits(isArithmetic, Element) ) { >>>> ret ~= toString(this[i,j]) ~ "\t"; >>>> } else { >>>> ret ~= this[i,j].toString ~ "\t"; >>>> } >>>> >>>> It works but it is wrong >>> I believe this works: >>> >>> static if (is(typeof(this[i,j].toString))) { >>> ret ~= this[i,j].toString; >>> } else { >>> ret ~= toString(this[i,j]); >>> } >>> >> No, this cause error: >> >> toString () does not match parameter types (float) >> >> ( condition is always resolved to else {...} ) > > That's because float's don't have a toString method. So it should be > picking the else{} case. > > If you try it with something where this[i,j] returns an Object or > struct with a toString, then it should pick the 'if' branch. There was confusion between this.toString () and std.stdio.toString () Fixed, thanks!
How I can determine this is class or struct at compile time?
subj
Re: compile time method check
Simen Kjaeraas пишет: > On Fri, 26 Dec 2008 20:15:30 +0100, Weed wrote: > >> Can I at compile time check whether there is a facility method >> (toString(), for example)? >> >> Today I wrote: >> >> static if ( __traits(isArithmetic, Element) ) { >> ret ~= toString(this[i,j]) ~ "\t"; >> } else { >> ret ~= this[i,j].toString ~ "\t"; >> } >> >> It works but it is wrong > > I believe this works: > > static if (is(typeof(this[i,j].toString))) { > ret ~= this[i,j].toString; > } else { > ret ~= toString(this[i,j]); > } > No, this cause error: toString () does not match parameter types (float) ( condition is always resolved to else {...} )
compile time method check
Can I at compile time check whether there is a facility method (toString(), for example)? Today I wrote: static if ( __traits(isArithmetic, Element) ) { ret ~= toString(this[i,j]) ~ "\t"; } else { ret ~= this[i,j].toString ~ "\t"; } It works but it is wrong
Re: struct inheritance need?
Kagamin пишет: > Weed Wrote: > >> If you do not want to initialize repeatedly matrix inside the sub, which >> often cause each other, must be static matrices or declared as global >> (in relation to these procedures). You agree with that? > > What's problem? If you want static or global variables, you have them in D. > >> Do you propose through mixin divide the code into parts? > > Well, why mixin? Just create different modules and place code according to > its purpose to these modules. > >> You do not think that it is better to add to language that would give >> 100% to use the opportunity of calculations at compile time. This >> greatly simplify the code and perfectly and logically fit into the language. > > Compile-time evaluation is just an optimization technique, which may or may > not be applied without any change to source code, Incidentally, this is incorrect. In the event that it is impossible to calculate the static expression compiler immediately gives an error rather than moves calculation to run-time.
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: If you do not want to initialize repeatedly matrix inside the sub, which often cause each other, must be static matrices or declared as global (in relation to these procedures). You agree with that? What's problem? If you want static or global variables, you have them in D. Do you propose through mixin divide the code into parts? Well, why mixin? Just create different modules and place code according to its purpose to these modules. You do not think that it is better to add to language that would give 100% to use the opportunity of calculations at compile time. This greatly simplify the code and perfectly and logically fit into the language. Compile-time evaluation is just an optimization technique, which may or may not be applied without any change to source code, so it doesn't affect source code in any way, it just can't. In C++ you can't tell whether the code executes at compile time, as as of standard, it's completely up to the compiler to optimize code generation. Yeah, right. I propose that it be expanded to optimize the static arrays and classes.
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: too cannot be initialized in a compile time. Sure it can't. Does it cause that big problems? Sometimes it is the only way to avoid a large number of global ad In D module variables can be protected by access modifiers and become module-local. Module full of mathematics turns into horror quite quickly. I have checked this:) If you have troubles with understanding big modules, you can split them into smaller modules, which will be easier to understand for sure. If you do not want to initialize repeatedly matrix inside the sub, which often cause each other, must be static matrices or declared as global (in relation to these procedures). You agree with that? Do you propose through mixin divide the code into parts? You do not think that it is better to add to language that would give 100% to use the opportunity of calculations at compile time. This greatly simplify the code and perfectly and logically fit into the language.
Re: struct inheritance need?
Weed пишет: Kagamin пишет: Weed Wrote: I'd prefer run-time checks, though templates can be used for sure. This problem would help solve the availability of inheritance for structs or compile-time creation of class instances. And I see no problem. Absence of compile-time object creation doesn't prevent you from using templates. The problem is not in use templates. Templates are implementing some of the functionality of 2 types of structures matrices (normal and dynamic). But the structures do not inherit, then to add functionality matrix to other entities ( "pixel", "image" etc) sites will have their list in a template return type. For example: http://www.dsource.org/projects/openmeshd/browser/trunk/LinAlg/linalg/MatrixT.d see template MultReturnType(ArgT) It contain list of all types (MatrixT and VectorT) for return. Will it add types of "image" and "pixel" and still others if needed. This is as good as manually implement a new object model. It is not necessary to suggest to wrap up "pixel" in a class - then it too cannot be initialized in a compile time. In fact, the minimum number of matrices 3: dynamic fixed-size fixed-size static The last 2 are different way of storing components - "fixed-size static" has dynamic array (because compile-time assignment is now does not assign anything to static array) and "fixed-size" it with a static array with size calculated in the template. I will be happy if someone tells a mistake in the design of my idea :)
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: It is not necessary to suggest to wrap up "pixel" in a class - then it too cannot be initialized in a compile time. Sure it can't. Does it cause that big problems? Sometimes it is the only way to avoid a large number of global ad In D module variables can be protected by access modifiers and become module-local. Module full of mathematics turns into horror quite quickly. I have checked this:)
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: The problem is not in use templates. Templates are implementing some of the functionality of 2 types of structures matrices (normal and dynamic). But the structures do not inherit, then to add functionality matrix to other entities ( "pixel", "image" etc) sites will have their list in a template return type. If structs don't suit you, don't use them. Classes are better suited for OOP as I said long ago and continue repeating it over and over. It contain list of all types (MatrixT and VectorT) for return. Will it add types of "image" and "pixel" and still others if needed. This is as good as manually implement a new object model. I'm sure any properly formalized problem is solvable. All you need is proper formalization and some design work. It is not necessary to suggest to wrap up "pixel" in a class - then it too cannot be initialized in a compile time. Sure it can't. Does it cause that big problems? Sometimes it is the only way to avoid a large number of global ad
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: I'd prefer run-time checks, though templates can be used for sure. This problem would help solve the availability of inheritance for structs or compile-time creation of class instances. And I see no problem. Absence of compile-time object creation doesn't prevent you from using templates. The problem is not in use templates. Templates are implementing some of the functionality of 2 types of structures matrices (normal and dynamic). But the structures do not inherit, then to add functionality matrix to other entities ( "pixel", "image" etc) sites will have their list in a template return type. For example: http://www.dsource.org/projects/openmeshd/browser/trunk/LinAlg/linalg/MatrixT.d see template MultReturnType(ArgT) It contain list of all types (MatrixT and VectorT) for return. Will it add types of "image" and "pixel" and still others if needed. This is as good as manually implement a new object model. It is not necessary to suggest to wrap up "pixel" in a class - then it too cannot be initialized in a compile time.
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: that is, suppose that after some action should get a matrix matrix3x1 Well... if you want to template every piece of your code, this can cause disaster, so I think, this is not very good design. For example, multiplication method will be duplicated N*N*N times for all possible matrix size combinations. Only for really used combinations (i am using "duck typing" inside templates). In any case, unused functions in the resulting file is not included. I'd prefer run-time checks, though templates can be used for sure. This problem would help solve the availability of inheritance for structs or compile-time creation of class instances. But now the compiler can identify duplicate parts of the code, working with the same types
Re: struct inheritance need?
Weed пишет: Kagamin пишет: Weed Wrote: Sure he wants. From my point of view, this technique is supposed to be a means to solve some problem rather than problem itself. But this problem was not put. please read it thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=80945 The problem of matrix of arbitrary size is solved by size-agnostic base class whose interface is very similar to that of dynamic matrix. If 2 predetermined matrix certain size multiplied, the produced matrix must be a certain size rather than dynamic. ...and a matrix of static size in the same used in other objects - matrix functional mixed into this objects by mixin. that is, suppose that after some action should get a matrix matrix3x1 You can not assign it to "pixel" struct, you will receive matrix3x1 and make copy of "pixel".
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: Sure he wants. From my point of view, this technique is supposed to be a means to solve some problem rather than problem itself. But this problem was not put. please read it thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=80945 The problem of matrix of arbitrary size is solved by size-agnostic base class whose interface is very similar to that of dynamic matrix. If 2 predetermined matrix certain size multiplied, the produced matrix must be a certain size rather than dynamic.
Re: struct inheritance need?
Kagamin пишет: Derek Parnell Wrote: I think he wants to have some objects constructed at compile-time. Sure he wants. From my point of view, this technique is supposed to be a means to solve some problem rather than problem itself. But this problem was not put. please read it thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=80945
Re: struct inheritance need?
Kagamin пишет: Derek Parnell Wrote: I think he wants to have some objects constructed at compile-time. Sure he wants. From my point of view, this technique is supposed to be a means to solve some problem rather than problem itself. But this problem was not put. Please read even this edition: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=80945
Re: struct inheritance need?
Derek Parnell пишет: On Thu, 18 Dec 2008 07:24:34 -0500, Kagamin wrote: Static constructor can execute any valid D statements including construction of objects. A static constructor (also known as the Module constructor) executes at program run-time and not at program compile-time. I think Weed wants the ability to have the compiler build class objects at compile-time such that when a program first starts running, the objects are already fully formed in RAM just waiting to be used. It is exactly
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: Kagamin пишет: Weed Wrote: Global variables are static members of module, wich is similar to class and also has static constructor. So what? So your problem is solved. If everything was so simple! :) Once again: the static constructor of class NOT constructs an object. Static constructor can execute any valid D statements including construction of objects. The static constructor does not take *this*. Therefore, it can not create instance of object. In your case it initiates the external variable. This works: --- import std.stdio; class Matrix { int i; this(int j) { i=j; } } Matrix m; static this() { m=new Matrix(7); } void main() { writeln(m.i); } ---
Re: struct inheritance need?
Weed пишет: Kagamin пишет: Weed Wrote: Global variables are static members of module, wich is similar to class and also has static constructor. So what? So your problem is solved. If everything was so simple! :) Once again: the static constructor of class NOT constructs an object. He just fills the static variables of class. These variables are common to all objects of class. *It* just fills the static variables of class. These variables are common to all objects of class. sorry:)
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: Global variables are static members of module, wich is similar to class and also has static constructor. So what? So your problem is solved. If everything was so simple! :) Once again: the static constructor of class NOT constructs an object. He just fills the static variables of class. These variables are common to all objects of class.
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: There does not need a static initialization of static members of the class. There must be able to create objects of classes at compile time. Well, these objects should be placed in some static variables, right? Yes Static variables are initialized with static constructors. Yes, static variables of class are initialized with static constructor of class. Global variables are static members of module, wich is similar to class and also has static constructor. So what?
Re: struct inheritance need?
Janderson пишет: Weed wrote: Janderson пишет: Weed wrote: I should explain why it's important for me: For example, I am making a matrix object(s) It should be: - any size - with ability of making matrix instance of a given size in compile time. - ability of creating matrix instance in runtime. I have decided to make it struct because I need to create matrix object in compile time. in real program I'll need matricies of variable size and 3х1, 3х3, 6х6, 3х6 sizes (and may be other). I want to make matrix template and insert in it with mixin operator several structures of different sizes (and this structs are not store his dimensions in each instance, of course: variables width and height declared as invariant). By doing this I'll get several different structures (matrix_dynamic, matrix3x3, matrix6x6 etc) question is how those matricies can interoperate? They does not have common parent (structures does not have inheritance) , so I can't make common function for matricies of different sizes opAdd for example, and have to do functions for all types of matricies. How should I implement such a class In D properly? It possible in C++, but I like some of the language D The classical approach is to have "helper" template functions. Essentially: void doOpearation(mymatrix)(...) { } If I create struct MatrixStruct for compile-time matrix and class MatrixClass for all other I will not be able to create a function of interaction between these objects through the templates because some of them will be announced before the other and it will not be able to get another type of object in a template. (I do not know, understand this long phrase on my strange English or not? : )) Note this is compiletime polymorphisms. Now if you want to add runtime polymorphism it is impossible to get away from a cost. You can use a wrapper class like so: interface CommonMatrix { operation()... } Also, interfaces can not be used with the structs I'm not sure what you mean: I meant this: You have your struct: struct Foo { } Then you have your template wrapper class DoDynamicPolymorpicStuff(T) : TheInterface { T foo; operation() { foo; } } Its a reasonably common pattern. How do you implement matrix data of this class in compile-time in the case of matrix is fixed-sized? Whether you can start immediately after the program began to operate this class matrix as if it was created at compile time? class PolymorphicMatrix(mymatrix) : CommonMatrix { operation()... } You'll probably find though that PolymorphicMartix should be some higher level concept, like car, or entity. The great thing about these techniques is that they give a whole load more power then just using inheritance alone because you can essentially attach many different behaviors to that struct. I hope that was helpful. -Joel
Re: struct inheritance need?
Weed пишет: Christopher Wright пишет: Weed wrote: If I create struct MatrixStruct for compile-time matrix and class MatrixClass for all other I will not be able to create a function of interaction between these objects through the templates because some of them will be announced before the other and it will not be able to get another type of object in a template. (I do not know, understand this long phrase on my strange English or not? : )) What operations do you need at compile time and what operations do you need at runtime? Compile-time creation an object of class or (most likely wrong) struct inheritance. I have prepared a distinct feature request and send it later You can read this in the digitalmars.D, topic: Feature request: Deploying a class instance in the default data segment
Re: struct inheritance need?
Christopher Wright пишет: Weed wrote: If I create struct MatrixStruct for compile-time matrix and class MatrixClass for all other I will not be able to create a function of interaction between these objects through the templates because some of them will be announced before the other and it will not be able to get another type of object in a template. (I do not know, understand this long phrase on my strange English or not? : )) What operations do you need at compile time and what operations do you need at runtime? Compile-time creation an object of class or (most likely wrong) struct inheritance. I have prepared a distinct feature request and send it later
Re: struct inheritance need?
Christopher Wright пишет: Weed wrote: If I create struct MatrixStruct for compile-time matrix and class MatrixClass for all other I will not be able to create a function of interaction between these objects through the templates because some of them will be announced before the other and it will not be able to get another type of object in a template. (I do not know, understand this long phrase on my strange English or not? : )) What operations do you need at compile time and what operations do you need at runtime? Compile time creating an object of class or (most likely wrong) struct inheritance. I have prepared a distinct feature request, send it later
Re: struct inheritance need?
Janderson пишет: Weed wrote: I should explain why it's important for me: For example, I am making a matrix object(s) It should be: - any size - with ability of making matrix instance of a given size in compile time. - ability of creating matrix instance in runtime. I have decided to make it struct because I need to create matrix object in compile time. in real program I'll need matricies of variable size and 3х1, 3х3, 6х6, 3х6 sizes (and may be other). I want to make matrix template and insert in it with mixin operator several structures of different sizes (and this structs are not store his dimensions in each instance, of course: variables width and height declared as invariant). By doing this I'll get several different structures (matrix_dynamic, matrix3x3, matrix6x6 etc) question is how those matricies can interoperate? They does not have common parent (structures does not have inheritance) , so I can't make common function for matricies of different sizes opAdd for example, and have to do functions for all types of matricies. How should I implement such a class In D properly? It possible in C++, but I like some of the language D The classical approach is to have "helper" template functions. Essentially: void doOpearation(mymatrix)(...) { } If I create struct MatrixStruct for compile-time matrix and class MatrixClass for all other I will not be able to create a function of interaction between these objects through the templates because some of them will be announced before the other and it will not be able to get another type of object in a template. (I do not know, understand this long phrase on my strange English or not? : )) Note this is compiletime polymorphisms. Now if you want to add runtime polymorphism it is impossible to get away from a cost. You can use a wrapper class like so: interface CommonMatrix { operation()... } Also, interfaces can not be used with the structs class PolymorphicMatrix(mymatrix) : CommonMatrix { operation()... } You'll probably find though that PolymorphicMartix should be some higher level concept, like car, or entity. The great thing about these techniques is that they give a whole load more power then just using inheritance alone because you can essentially attach many different behaviors to that struct. I hope that was helpful. -Joel
Re: struct inheritance need?
Weed пишет: Kagamin пишет: Weed Wrote: I agree. In my case I chose to structure rather than a class because it can be initialized at compile time. But now I thing must be allowed to deploy class in the default data segment. And add the possibility of creating a object of class at compile time. If you want to use full blown OOP, class is your choise. There are static constructors for static initialization. There does not need a static initialization of static members of the class. There must be able to create objects of classes at compile time. === Now what? How to initiate the procedure for adding this feature?
Re: struct inheritance need?
Kagamin пишет: Weed Wrote: I agree. In my case I chose to structure rather than a class because it can be initialized at compile time. But now I thing must be allowed to deploy class in the default data segment. And add the possibility of creating a object of class at compile time. If you want to use full blown OOP, class is your choise. There are static constructors for static initialization. There does not need a static initialization of static members of the class. There must be able to create objects of classes at compile time.
Re: struct inheritance need?
Yigal Chripun пишет: If I understand you correctly - I think you confuse here two separate and orthogonal issues. 1) struct vs. class 2) memory allocation What D tries to do is to provide types with value semantics via structs and types with reference semantics _and_polymorphism_ via classes. IMO C++ is a prime example how to not design a language and the slicing problem is a good example of that. value types should not have polymorphism whatsoever as is done in D. memory allocation is orthogonal to this: class C {...} Struct S {...} auto c1 = new C; // classes default to heap alloc scope c2 = new C; // c2 is on stack S s1; // structs default to stack auto s2 = new S; // s2 is S* (heap alloc) I think Andrei said he wants to make changes to "scope", that is IIRC. What will change? struct inheritance, if it gets implemented in D, should express IMO semantics similar to concepts as in C++0x, not provide polymorphism for value types which is clearly a mistake. (also this will remove the need for typedefs, isn't it?) I agree. In my case I chose to structure rather than a class because it can be initialized at compile time. But now I thing must be allowed to deploy class in the default data segment. And add the possibility of creating a object of class at compile time. Nothing prevents this change?
Re: struct inheritance need?
bearophile пишет: > Weed: >> Planned in the future to implement inheritance of structs or the static creation of classes? > > Inheritance of structs: I think it's not planned. Structs in D are meant to be used for different things than classes. > Yet, as time passes structs are gaining more power: you can't believe > that very recently they have gained constructors/destructors too in D2. Probably in a system language conceptual purity isn't much appreciated :-) > I believe that the opportunity to place an object in memory, stack or heap is more important than the struggle against "splicing". I think not worth taking structs and classes from C#. May be bytecode interpreter C# does not show the difference in speed between the allocation of memory by a small object + its using and the use of a static object, so developers C# decided to do so as done. (but I am not a specialist in the design of compilers :)) > Static creation of classes (I think you mean creation of objects): it > sounds like an interesting thing, I know of a system language that > allows the creation of objects only at compile-time, and at > compile-time it also performs several space optimizations among such > objects (and such space optimizations often improve running speed a > little). And in fact we come to making structs and classes similar except that classes can not be assigned by value. Such an option I like.
Re: distinguish between classes and structures
Kagamin пишет: Weed Wrote: it is impossible to create the struct of the object which will be arbitrary size (raised by a user) + to be able to set compile-time + should work with the structures of its type. this is mathematical matrix, for example Matix? Easy. class Matrix //can inherit from some generic base class { int[2] dims; int[][] data; //methods } not just a matrix, see thread "struct inheritance need?" in this NG
Re: struct inheritance need?
Weed пишет: I should explain why it's important for me: For example, I am making a matrix object(s) It should be: - any size - with ability of making matrix instance of a given size in compile time. - ability of creating matrix instance in runtime. I have decided to make it struct because I need to create matrix object in compile time. in real program I'll need matricies of variable size and 3х1, 3х3, 6х6, 3х6 sizes (and may be other). I want to make matrix template and insert in it with mixin operator several structures of different sizes (and this structs are not store his dimensions in each instance, of course: variables width and height declared as invariant). By doing this I'll get several different structures (matrix_dynamic, matrix3x3, matrix6x6 etc) question is how those matricies can interoperate? They does not have common parent (structures does not have inheritance) , so I can't make common function for matricies of different sizes opAdd for example, and have to do functions for all types of matricies. How should I implement such a class In D properly? It possible in C++, but I like some of the language D I think could help a static creation objects of classes (including compile-time creation), but it is also impossible now. For example: MatrixClass a(3,2); static MatrixClass a(3,2); Planned in the future to implement inheritance of structs or the static creation of classes?
Re: struct inheritance need?
Bill Baxter пишет: 2008/12/16 Weed : I should explain why it's important for me: For example, I am making a matrix object(s) It should be: - any size - with ability of making matrix instance of a given size in compile time. - ability of creating matrix instance in runtime. I have decided to make it struct because I need to create matrix object in compile time. in real program I'll need matricies of variable size and 3х1, 3х3, 6х6, 3х6 sizes (and may be other). I want to make matrix template and insert in it with mixin operator several structures of different sizes (and this structs are not store his dimensions in each instance, of course: variables width and height declared as invariant). By doing this I'll get several different structures (matrix_dynamic, matrix3x3, matrix6x6 etc) question is how those matricies can interoperate? They does not have common parent (structures does not have inheritance) , so I can't make common function for matricies of different sizes opAdd for example, and have to do functions for all types of matricies. How should I implement such a class In D properly? It possible in C++, but I like some of the language D Here's one way: http://www.dsource.org/projects/openmeshd/browser/trunk/LinAlg/linalg/MatrixT.d --bb necessarily need the ability to change the size of the matrix together with a static matrix it can be done using two types of matrices with a single parent.
Re: struct inheritance need?
Bill Baxter пишет: 2008/12/16 Weed : I should explain why it's important for me: For example, I am making a matrix object(s) It should be: - any size - with ability of making matrix instance of a given size in compile time. - ability of creating matrix instance in runtime. I have decided to make it struct because I need to create matrix object in compile time. in real program I'll need matricies of variable size and 3х1, 3х3, 6х6, 3х6 sizes (and may be other). I want to make matrix template and insert in it with mixin operator several structures of different sizes (and this structs are not store his dimensions in each instance, of course: variables width and height declared as invariant). By doing this I'll get several different structures (matrix_dynamic, matrix3x3, matrix6x6 etc) question is how those matricies can interoperate? They does not have common parent (structures does not have inheritance) , so I can't make common function for matricies of different sizes opAdd for example, and have to do functions for all types of matricies. How should I implement such a class In D properly? It possible in C++, but I like some of the language D Here's one way: http://www.dsource.org/projects/openmeshd/browser/trunk/LinAlg/linalg/MatrixT.d --bb necessarily need the ability to change the size of the matrix together with a static matrix it can be done using two types of matrices with a common parent.
struct inheritance need?
I should explain why it's important for me: For example, I am making a matrix object(s) It should be: - any size - with ability of making matrix instance of a given size in compile time. - ability of creating matrix instance in runtime. I have decided to make it struct because I need to create matrix object in compile time. in real program I'll need matricies of variable size and 3х1, 3х3, 6х6, 3х6 sizes (and may be other). I want to make matrix template and insert in it with mixin operator several structures of different sizes (and this structs are not store his dimensions in each instance, of course: variables width and height declared as invariant). By doing this I'll get several different structures (matrix_dynamic, matrix3x3, matrix6x6 etc) question is how those matricies can interoperate? They does not have common parent (structures does not have inheritance) , so I can't make common function for matricies of different sizes opAdd for example, and have to do functions for all types of matricies. How should I implement such a class In D properly? It possible in C++, but I like some of the language D
Re: distinguish between classes and structures
Bill Baxter пишет: 2008/12/16 Weed : Frits van Bommel пишет: Weed wrote: In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // error This is why: s1 val2 = *base_ptr; // error I do not understand that it is wrong? The problem is that base_ptr isn't actually pointing to an s1. So by dereferencing it and assigning to an s1 you just sliced it. Any extra data derr had is lost. Any method overrides it had are lost. So to avoid slicing you would need to disallow dereferencing struct pointers, or disallow pointing to a struct of a different type. Disallowing dereferencing a struct pointer seems like it might be possible. But then again that's also kinda what class gives you already. And then you'd be left with no way to create a pointer that can be turned back into a value! That doesn't sound so good. --bb Pointers are dangerous. Does this mean that the pointers will be removed from the language?
Re: distinguish between classes and structures
Bill Baxter пишет: 2008/12/16 Weed : Frits van Bommel пишет: Weed wrote: In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // error This is why: s1 val2 = *base_ptr; // error I do not understand that it is wrong? The problem is that base_ptr isn't actually pointing to an s1. So by dereferencing it and assigning to an s1 you just sliced it. This break garbage collection? Or what? Any extra data derr had is lost. Any method overrides it had are lost. So to avoid slicing you would need to disallow dereferencing struct pointers, or disallow pointing to a struct of a different type. Disallowing dereferencing a struct pointer seems like it might be possible. But then again that's also kinda what class gives you already. And then you'd be left with no way to create a pointer that can be turned back into a value! That doesn't sound so good.
Re: distinguish between classes and structures
Bill Baxter пишет: 2008/12/16 Weed : Frits van Bommel пишет: Weed wrote: In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // error This is why: s1 val2 = *base_ptr; // error I do not understand that it is wrong? The problem is that base_ptr isn't actually pointing to an s1. So by dereferencing it and assigning to an s1 you just sliced it. Any extra data derr had is lost. Any method overrides it had are lost. So to avoid slicing you would need to disallow dereferencing struct pointers, or disallow pointing to a struct of a different type. Disallowing dereferencing a struct pointer seems like it might be possible. But then again that's also kinda what class gives you already. And then you'd be left with no way to create a pointer that can be turned back into a value! That doesn't sound so good. --bb Classes can not be created at compile time. D, with its templates would do it perfectly for structs.
Re: distinguish between classes and structures
Frits van Bommel пишет: Weed wrote: In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // error This is why: s1 val2 = *base_ptr; // error Yes, right. I do not understand that it is wrong? > > (And disallowing '*ptr' on struct pointers is not likely to find much > support) I am not saying do not let '* ptr'. Besides, now you can not do this just because there is no inheritance at all.
Re: distinguish between classes and structures
Kagamin пишет: Weed Wrote: Without inheritance of structs many things are not possible, compared with C++. for example? it is impossible to create the struct of the object which will be arbitrary size (raised by a user) + to be able to set compile-time + should work with the structures of its type. this is mathematical matrix, for example
Re: distinguish between classes and structures
Frits van Bommel пишет: Weed wrote: In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // error This is why: s1 val2 = *base_ptr; // error I do not understand that it is wrong? (And disallowing '*ptr' on struct pointers is not likely to find much support)
Re: distinguish between classes and structures
Weed пишет: Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. It's a question: Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value?
Re: distinguish between classes and structures
Bill Baxter пишет: 2008/12/15 Weed : Bill Baxter пишет: On Mon, Dec 15, 2008 at 3:35 PM, Weed wrote: Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English) The main justification is eliminating the slicing problem. http://cplusplusgems.blogspot.com/2005/10/what-is-slicing-problem-class-base.html D solves it by making it impossible to have a class instance as a value type. Why not disallow the casting for structs, leaving the possibility of up casting for the links and pointers to the structure? I have to confess I don't really understand this question. Can you rephrase or give an example? Sorry, English is not my native language In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // error
Re: distinguish between classes and structures
Weed пишет: Bill Baxter пишет: On Mon, Dec 15, 2008 at 3:35 PM, Weed wrote: Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English) The main justification is eliminating the slicing problem. http://cplusplusgems.blogspot.com/2005/10/what-is-slicing-problem-class-base.html D solves it by making it impossible to have a class instance as a value type. Why not disallow the casting for structs, leaving the possibility of up casting for the links and pointers to the structure? What is the best place for such questions?
Re: distinguish between classes and structures
Bill Baxter пишет: On Mon, Dec 15, 2008 at 3:35 PM, Weed wrote: Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English) The main justification is eliminating the slicing problem. http://cplusplusgems.blogspot.com/2005/10/what-is-slicing-problem-class-base.html D solves it by making it impossible to have a class instance as a value type. Why not disallow the casting for structs, leaving the possibility of up casting for the links and pointers to the structure?
distinguish between classes and structures
Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English)
Why I can't use array in compile-time struct creation?
It is forbidden by specification, but an interesting reason. for example: struct S { static S opCall() { S res; int a,b,c,d; int[10] data; // If I comment out this line compilation goes smoothly return res; } } void main() { static S s = S(); } struct_array.d(14): Error: cannot evaluate opCall() at compile time struct_array.d(14): Error: cannot evaluate opCall() at compile time
Re: Why is my code does not compile? (dmd 2.021 linux)
Bill Baxter пишет: Ref return values are a very new feature in D2. I suspect its just a bug that they don't work on inner functions yet. --bb message implies that the authors are aware of this limitation? report they do not need? No, a report is probably needed. There are lots of bugs with the new const and ref features in D2. Many have been reported, but there are still many more to be discovered. Returning refs in particular is very new, so I would expect bugs a plenty with that, though I think few have been reported so far. I don't keep up with the bugs newsgroup these days, though. Certainly try to search for it in the bug db before filing a new report, though. D's bugzilla is here: http://d.puremagic.com/issues/ Posted here: http://d.puremagic.com/issues/show_bug.cgi?id=2509
Re: Why is my code does not compile? (dmd 2.021 linux)
Bill Baxter пишет: On Sat, Dec 13, 2008 at 5:45 AM, Weed wrote: void main() { int i; ref int func() { return i; } func() = 4; } lval.d(5): found 'ref' instead of statement lval.d(10): no identifier for declarator func lval.d(11): unrecognized declaration but code like this compiles: ref int func() { int* i = new int; return *i; } void main() { func() = 4; } Ref return values are a very new feature in D2. I suspect its just a bug that they don't work on inner functions yet. --bb message implies that the authors are aware of this limitation? report they do not need?
Why is my code does not compile? (dmd 2.021 linux)
void main() { int i; ref int func() { return i; } func() = 4; } lval.d(5): found 'ref' instead of statement lval.d(10): no identifier for declarator func lval.d(11): unrecognized declaration but code like this compiles: ref int func() { int* i = new int; return *i; } void main() { func() = 4; }
Re: Why is my structure template does not compile?
BCS пишет: Reply to Denis, On Fri, 12 Dec 2008 02:21:11 +0300, BCS wrote: Reply to Weed, invariant() { // If I comment out next line compilation goes smoothly: assert( Element.sizeof > 0 ); } OTOH that assert is wrong. Element.sizeof will always return 8, the size of an array reference. What you want is Element.length. No, I don't think so. Don't confuse 'Element' with 'data'. Element is a type, it doesn't have a length property. But it does have sizeof (an example of Element is an 'int', which is 4 bytes long). Oh, fud. I did get that wrong. I still think it's wrong as I'm not sure anything has 0 sizeof and even then that should be static assert. You are right. It happens that the verification Element not null. static assert would be better.
Re: Why is my structure template does not compile?
ref Element opIndexAssign( in Element a, in uint n ) { data[n] += a; return data[n]; } I'm guessing as I don't use 2.0 but I think that this is a bug. DMD is trying to say that the above returns are trying to return something that can't be referenced (like a math expression result). It is a bug, indeed. The struct 'invariant' prevents proper template instantiation somehow... Removing it makes code work as intended. I'll submit a bug report. Thank you! Give a link to the bugreport later?
Why is my structure template does not compile?
struct S( Element ) { Element[] data; this( in uint len ) { data.length = len; } ref Element opIndex( in uint n ) { return data[n]; } ref Element opIndexAssign( in Element a, in uint n ) { data[n] += a; return data[n]; } invariant() { // If I comment out next line compilation goes smoothly: assert( Element.sizeof > 0 ); } } void main() { alias S!(double) st; st test = st(20); auto a = test[2]; test[2] = 3; } compilation error: $ dmd demostruct Error: __result = this is not an lvalue demostruct.d(12): Error: __result = (this.data[n]) is not an lvalue demostruct.d(18): Error: __result = (this.data[n]) is not an lvalue demostruct.d(29): template instance demostruct.S!(double) error instantiating
Re: static initialization question
Jarrett Billingsley пишет: On Wed, Dec 10, 2008 at 5:31 PM, Weed <[EMAIL PROTECTED]> wrote: code: import std.stdio; class MyClass { invariant uint a = 0; } void main() { static MyClass c = new MyClass; writeln( c.a ); } It's not the class member that wants static initialization, it's your variable declaration. static MyClass c = new MyClass; This is illegal because static variables must be initialized with compile-time constants. The simple way around this is: static MyClass c; // defaults to null c = new MyClass; Which separates the declaration from initialization. In C++ analogous construction means creation of uninitialized static pointer (in compile time) and it's initialization at first execution of this line in the function. Why D does not behave that way on this construction?
Re: static initialization question
Denis Koroskin пишет: On Thu, 11 Dec 2008 01:31:32 +0300, Weed <[EMAIL PROTECTED]> wrote: But my class does not contain data that need initialization and can be created in compile time code: import std.stdio; class MyClass { invariant uint a = 0; } void main() { static MyClass c = new MyClass; writeln( c.a ); } There is a memory allocation that may occurs at run time only. In C++ analogous construction means creation of uninitialized static pointer (in compile time) and it's initialization at first execution this line in the function. Why D does not behave that way on this construction?
Re: static initialization question
code: import std.stdio; class MyClass { invariant uint a = 0; } void main() { static MyClass c = new MyClass; writeln( c.a ); }
static initialization question
moved from digitalmars.D > Re: static initialization questionDigital Mars > Benjamin Shropshire ([EMAIL PROTECTED]) 2008/12/10 16:46 > Reply to Weed, > >> why I can not initialize static variable this way: >> >> static MyClass c = new MyClass; >> >> Error: non-constant expression new MyClass >> >> (dmd 2.014) >> > > this NG is more or less dead. The digitalmars.d.learn NGs would be a better > choice. > > Only compile time constant values are legal for static initializers because > they are initialized at compile time as part of the static data segments. But my class does not contain data that need initialization and can be created in compile time