[Development] [QML] Assigning ints to enumeration properties

Olivier Goffart olivier at woboq.com
Wed Mar 6 10:48:36 CET 2013


On Tuesday 05 March 2013 13:34:14 Alan Alpert wrote:
> I'd like to make a slight change to enum property behavior in QML.
> Currently you get a compile error if you assign an int to an enum
> property, similar to the warning you'll get in C++ if you do the same.
> However in C++ you can easily work around this by casting it to the
> enum, or just ignoring the warning. Neither option is available in
> QML.

This is not a warning, this is an error in C++

enum Foo { A ,  B };
Foo bar = 1; // error: invalid conversion from ‘int’ to ‘Foo’
int blah = bar; // No warning. unless you use enum class then it is a error.

> Note that since this relaxes the constraints of the language,
> previously running QML will continue to run as before.
> 
> I'll enumerate the pros and cons:
> 
> Pros:
> -Matches expectations from C++ users a little better

If you mean implicit conversion, then they don't.

> -Matches expectations from script users a little better
> Cons:
> -Code is usually less readable when you use ints.
> -Less typesafe (although the actual type of enum isn't checked, merely
> that it is an enum, so it's not that big a loss).
> 
> And a further quirk, you can already get this behavior if you make
> your properties int properties instead of enums currently (because
> enums can be assigned to int properties). However this is ugly for
> classes which are also exposed as C++ APIs, as they'd much prefer to
> use enums for a good C++ API.
> 
> Since it's a bit of a borderline issue, with minimal gains or losses,
> I'd like some further opinions on the issue. Does anyone feel strongly
> for or against this change?

I think implicit casts is *not* a good idea.

Allowing explicit casts should be good.

-- 
Olivier 

Woboq - Qt services and support - http://woboq.com - http://code.woboq.org



More information about the Development mailing list