Sign in to follow this  
FDsagizi

auto as C++0x

Recommended Posts

FDsagizi    514

Hi smile.png

what you think about "auto" as c++ C++0x - this is difficult add to script ?

 

 

Exemple:

class SomeClassName
{    
    void DoSome(){}
}    

SomeClassName @Get()
{
    return ...
}

void F()
{
    auto a = Get();
    a.DoSome();
} 
 
Edited by FDsagizi

Share this post


Link to post
Share on other sites
Cornstalks    7030

Sorry, I'm not sure what you're asking. Personally, I only use auto for unweildly/very long typenames, like std::map<std::string, MyClass>::const_iterator, for example.

 

What's difficult about your code is that it's not clear what Get() returns (from just reading the function F()). If it were perhaps more appropriately named, it would help improve the clarity of your code.

 

Completely ignore this post! I didn't realize this was the AngleCode forum... sorry for being so blind! I thought FDsagizi might be asking what people thought of auto and if it was good to use it in the following code. I realize now that I wasn't even remotely close (and where's GameDev's embarrassed smiley!?).

Edited by Cornstalks

Share this post


Link to post
Share on other sites
WitchLord    4677

I think 'auto' is a useful feature in C++, especially when writing template functions, or even macros. 

 

In AngelScript I can see 'auto' being useful for the mixin classes, and as such I have it on my to-do list to study the possibility of implementing it, however its currently very low on my priority.

 

It's not an easy feature to implement. It would reverse the order things are evaluated in the compiler right now, as it is necessary to first determine the type of the expression and only then declare the actual variable. 

Share this post


Link to post
Share on other sites
Cornstalks    7030

Sorry, I'm not sure what you're asking

We're in the AngelCode sub-forum -- he's asking about adding C++0x-esque auto to AngelScript.

Oh... wow... I'm an idiot. Sorry folks!

Share this post


Link to post
Share on other sites
saejox    714

I used to think type inference would be a great addition to AngelScript.

As i used it more and more in C++, i have turned around.

I see two problems with it.

 

1. AngelScript does not have templates or generics. It doesn't have nested classes neither.

It's not possible to create conventional iterators.

Auto is practically useless without long type names to infer.

 

2. Auto is not magic. You still need to determine if it's an handle, reference or value.

This is a great source of confusion, unwanted constructor/destructor calls and bunch of other nasty bugs.

Share this post


Link to post
Share on other sites
WitchLord    4677

AngelScript's mixin classes are generics, and auto can definitely come in handy in such cases. Example:

 

mixin class MyGenericImpl
{
   void func()
   {
      auto c = a + b;
      DoSomething(c);
   }
}
 
class A : MyGenericImpl
{
  int a, b;
}
 
class B : MyGenericImpl
{
  float a, b;
}

 

The variable c in the func() method will have the int type in class A, and the float type in class B, as the code for the mixin class is only compiled in the context where it was included.

 

 

In my opinion, auto is not supposed to be used to infer long type names. That should be done with the use of typedefs to declare a shorter alias for the long types. Currently AngelScript only supports creating aliases for primitive types with typedefs, but eventually I'll allow any type to be used in typedefs.

 

The use of auto outside generic code is not something I recommend, for much the same reasons that saejox mentioned. It would make it much harder to know what type is actually acted upon, thus make it more difficult to read the code.

Share this post


Link to post
Share on other sites
saejox    714

That's an interesting use of mixins :)

I didn't go beyond code reuse and separating same class in multiple files.

 

I have just read your ideas page.

I thought i should make a list myself. :)

 

1. Function pointers and function objects

2. Template objects with multiple subtypes

3. Inheritance from registered classes. Without this your whole program design changes, it sucks.

4. Closures/Anonymous functions/Lambdas. Functional programming is the best.

5. Builtin array, set and maps. Everyone needs them, why are they addons. They can still be optional, but builtin is a must. If they are builtin i can finally write some AngelScript only programs. I can write a 1000 word essay how important built-in containers are for a language.

6. Static functions. Great for organization.

7. Generic handle. like ref addon but supports all types and can implicitly convert.

8. Varidic functions. Or functions with argument array. void MyFunction(args). Would be easy with #7

9. Type comparison. if( typeof(my_int) == int ). This may even be a switch/select statement. Useless without #8

10. Unsafe keyword. Mark functions and objects to be free of garbage collection. Also warn user of the dangers. Maybe go crazy with pointers.

11. new and delete. Unmanaged memory for all types. ( would be double awesome with unsafe keyword)

12. Stateful functions. With a keyword like 'yield' . Pause execution, resume when called by other later. Script languages are great for stuff like this.

13. Reflection. No not as powerful as js or lua. At least functions like createClass .addMethod("func()") .invoke("func")

 

These are just stuff i like, no need for a feedback. You are likely very busy.

Share this post


Link to post
Share on other sites
WitchLord    4677

1. coming. delegates and also the opCall operator (same as C++ operator())

2. coming. though probably only as registered classes

3. I would like to do this, but I really don't see a proper solution for it. 

4. probably one day

5. no. I provide them as add-ons because not everyone has the same needs. The add-ons are as good as built-in, but more flexible as you can easily change them the way you want. The engine core shall provide the necessary hooks for the add-ons to work seemlessly

6. one day

7. add-on

8. one day

9. add-on

10 & 11. maybe. I do have an interest in adding support for pure pointers again, though with an engine property to turn it off where sandboxing is required

12. as much as possible in add-on, but with necessary support from engine

13. add-on with support for incremental compilation in the engine

Share this post


Link to post
Share on other sites
saejox    714

I think i can live without built-in containers. Still, i just feel addon's are not the same. VM could do much better job. Compiler time optimization, better memory layout, avoid unnecessary ref counting, weak iterators etc..

Anyway, that's what i think. I can't even write a simple parser. My suggestions are probably worthless :)

 

Delegates and multiple subtypes are going to be revolutionizing.

I will rewrite half my AngelScript code. Can wait :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this