enum questions

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

enum questions

postite
hello ,
I have a little question about enums (which i don't really understand th e power).
does enums can be strings ?
I wanted to make a wrapper for static String const

just a quick example :

enum Color{
red;
green;
blue;
}




class Test{


public function new(){
 testEnum(Color.green);
}

function testEnum ( c:Color){
AcceptOnlyString(c);
}

function AcceptOnlyString(s:String){
trace("force="+s);
}


}

AcceptOnlyString throws an error ...

What kind of enum should i use to do that ?
is it not possible ..?

Any tip on that ?

thx





--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

Julien CASTETS

Hi,

I don't understrand your point

Std.string(c);

should do the trick

Cheers
Julien

Le 29 juil. 2011 20:25, "david" <[hidden email]> a écrit :
> hello ,
> I have a little question about enums (which i don't really understand th e power).
> does enums can be strings ?

> I wanted to make a wrapper for static String const
>
> just a quick example :
>
> enum Color{
> red;
> green;
> blue;
> }
>
>

>
>
> class Test{
>
>
> public function new(){
> testEnum(Color.green);
> }
>
> function testEnum ( c:Color){
> AcceptOnlyString(c);
> }
>

> function AcceptOnlyString(s:String){
> trace("force="+s);
> }
>
>
> }
>
> AcceptOnlyString throws an error ...
>
> What kind of enum should i use to do that ?
> is it not possible ..?

>
> Any tip on that ?
>
> thx
>
>
>
>
>
> --
> haXe - an open source web programming language
> http://haxe.org

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

RE: enum questions

Lee Sylvester
In reply to this post by postite
Std.string(green);  // to output green as string.

Also, no need for Color.green... Just green will do.

Lee



-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of david
Sent: 29 July 2011 19:29
To: The haXe compiler list
Subject: [haXe] enum questions

hello ,
I have a little question about enums (which i don't really understand th e
power).
does enums can be strings ?
I wanted to make a wrapper for static String const

just a quick example :

enum Color{
red;
green;
blue;
}




class Test{


public function new(){
 testEnum(Color.green);
}

function testEnum ( c:Color){
AcceptOnlyString(c);
}

function AcceptOnlyString(s:String){
trace("force="+s);
}


}

AcceptOnlyString throws an error ...

What kind of enum should i use to do that ?
is it not possible ..?

Any tip on that ?

thx





--
haXe - an open source web programming language http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

postite
In reply to this post by Julien CASTETS
thanks julien

here is what i want to do :
i am making an JQuery extern class

i have a function with a string parameter

public function paf(option:String):JQuery

in the original javascript code , option can be "auto", "cool" and "hard" for example.


in my extern class i would like to limit the possibilities of the option to be sure to not make a typo .
I thought enum would be used for this but perhaps i am wrong.

so :

enum Option{
 auto;
cool;
hard;
}


so i could write my extern  function like this:

public function paf(option:Option):JQuery

but i cannot use Std.string(option) because it's an extern function .

Does i make myself clear ?
or it is absolutely not the way to do it ?

thanks for your help.

D
--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: RE: [haXe] enum questions

Julien CASTETS
In reply to this post by Lee Sylvester

Oh ok!

For flash externs i'm using @fakeenum metatdata maybe you could use them for js too
You will find some examples in the std.flash package

Hope it helps
Julien

Le 29 juil. 2011 20:45, "Lee Sylvester" <[hidden email]> a écrit :
> Std.string(green); // to output green as string.
>
> Also, no need for Color.green... Just green will do.
>
> Lee
>
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of david
> Sent: 29 July 2011 19:29
> To: The haXe compiler list
> Subject: [haXe] enum questions
>
> hello ,
> I have a little question about enums (which i don't really understand th e
> power).
> does enums can be strings ?
> I wanted to make a wrapper for static String const
>

> just a quick example :
>
> enum Color{
> red;
> green;
> blue;
> }
>
>
>
>
> class Test{
>
>
> public function new(){
> testEnum(Color.green);
> }
>
> function testEnum ( c:Color){
> AcceptOnlyString(c);
> }
>
> function AcceptOnlyString(s:String){
> trace("force="+s);
> }
>

>
> }
>
> AcceptOnlyString throws an error ...
>
> What kind of enum should i use to do that ?
> is it not possible ..?
>
> Any tip on that ?
>
> thx
>

>
>
>
>
> --
> haXe - an open source web programming language http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: RE: [haXe] enum questions

Julien CASTETS
Like this for instance

@:fakeEnum(String) enum VideoQuality {
small;
medium;
large;
hd720;
hd1080;
highres;
}

typedef YoutubePlayer = 
{
function cueVideoById(_VideoId:String, _StartSeconds:Float, _SuggestedQuality:VideoQuality):Void;
function loadVideoById(_VideoId:String, _StartSeconds:Float, _SuggestedQuality:VideoQuality):Void;
....

Cheers
Julien

2011/7/29 Julien CASTETS <[hidden email]>

Oh ok!

For flash externs i'm using @fakeenum metatdata maybe you could use them for js too
You will find some examples in the std.flash package

Hope it helps
Julien

Le 29 juil. 2011 20:45, "Lee Sylvester" <[hidden email]> a écrit :

> Std.string(green); // to output green as string.
>
> Also, no need for Color.green... Just green will do.
>
> Lee
>
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of david
> Sent: 29 July 2011 19:29
> To: The haXe compiler list
> Subject: [haXe] enum questions
>
> hello ,
> I have a little question about enums (which i don't really understand th e
> power).
> does enums can be strings ?
> I wanted to make a wrapper for static String const
>

> just a quick example :
>
> enum Color{
> red;
> green;
> blue;
> }
>
>
>
>
> class Test{
>
>
> public function new(){
> testEnum(Color.green);
> }
>
> function testEnum ( c:Color){
> AcceptOnlyString(c);
> }
>
> function AcceptOnlyString(s:String){
> trace("force="+s);
> }
>

>
> }
>
> AcceptOnlyString throws an error ...
>
> What kind of enum should i use to do that ?
> is it not possible ..?
>
> Any tip on that ?
>
> thx
>

>
>
>
>
> --
> haXe - an open source web programming language http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

postite

Thanks for the tip Julien ... i didn't know the existence of  @:fakeEnum ..
It seems to be built for what i attend to do But in my fake example:


@:fakeEnum(String) enum Baf {
red;
green;
blue;
}




class Test{


public function new(){
 testEnum(Baf.green);
}

function testEnum ( c:Baf){
type(c);
forceString(c);
}

function forceString(s:String){
trace("force="+s);
}


}

still not working...
And i don't want to cast .. it would be stupid ..
Can you help me on that ?

merci 
David

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

Nicolas Cannasse
In reply to this post by postite
Le 29/07/2011 20:49, david a écrit :

> thanks julien
>
> here is what i want to do :
> i am making an JQuery extern class
>
> i have a function with a string parameter
>
> public function paf(option:String):JQuery
>
> in the original javascript code , option can be "auto", "cool" and "hard" for example.

What you can do is the following :

enum Option {
        auto;
        cool;
        hard;
}

This will allow you to write Option.auto which will be an Option (and
not a string, so it can't be used as String in haXe code).

Then since you want it to be a String _at runtime only_, you can declare
the Option enum as "extern" and add somewhere in your code :

__js__('Option = { auto : "auto", cool : "cool", hard : "hard" };');

This will make sure that Option.auto == "auto"

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

postite


great it works!
thanks

by the way ,fake enum doesn't work for js output or is it just a specific behaviour for extern ?



Le 30 juil. 2011 à 13:10, Nicolas Cannasse a écrit :

> __js__('Option = { auto : "auto", cool : "cool", hard : "hard" };');


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

Nicolas Cannasse
Le 30/07/2011 13:40, david a écrit :
>
>
> great it works!
> thanks
>
> by the way ,fake enum doesn't work for js output or is it just a specific behaviour for extern ?

@:fakeEnum is only useful for Flash9, it tells that the actual type is a
String. It doesn't convert the enum in Strings itself since it's only
applied to extern enums.

Nicolas


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

postite
ok ! thx

>
> @:fakeEnum is only useful for Flash9, it tells that the actual type is a String. It doesn't convert the enum in Strings itself since it's only applied to extern enums.
>
> Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

bubblebenj
In reply to this post by Nicolas Cannasse
Is there an equivalent for the Neko target this hack 
enum Option {
       auto;
       cool;
       hard;
}
This will allow you to write Option.auto which will be an Option (and not a string, so it can't be used as String in haXe code).
Then since you want it to be a String _at runtime only_, you can declare the Option enum as "extern" and add somewhere in your code :

__js__('Option = { auto : "auto", cool : "cool", hard : "hard" };');
This will make sure that Option.auto == "auto"

Maybe using builtins ?

Ben


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

Cauê W.
you can always use @:native metadata, as previously stated. Though I don't know if it will work on C++ because seems cpp doesn't like classes defined as externs

2011/9/24 benjamin Dubois <[hidden email]>
Is there an equivalent for the Neko target this hack 

enum Option {
       auto;
       cool;
       hard;
}
This will allow you to write Option.auto which will be an Option (and not a string, so it can't be used as String in haXe code).
Then since you want it to be a String _at runtime only_, you can declare the Option enum as "extern" and add somewhere in your code :

__js__('Option = { auto : "auto", cool : "cool", hard : "hard" };');
This will make sure that Option.auto == "auto"

Maybe using builtins ?

Ben


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

bubblebenj
Sorry I'm not used to using metadata can you post an simple example please :)

On Sat, Sep 24, 2011 at 10:08 PM, Cauê Waneck <[hidden email]> wrote:
you can always use @:native metadata, as previously stated. Though I don't know if it will work on C++ because seems cpp doesn't like classes defined as externs

2011/9/24 benjamin Dubois <[hidden email]>
Is there an equivalent for the Neko target this hack 

enum Option {
       auto;
       cool;
       hard;
}
This will allow you to write Option.auto which will be an Option (and not a string, so it can't be used as String in haXe code).
Then since you want it to be a String _at runtime only_, you can declare the Option enum as "extern" and add somewhere in your code :

__js__('Option = { auto : "auto", cool : "cool", hard : "hard" };');
This will make sure that Option.auto == "auto"

Maybe using builtins ?

Ben


--

haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

Cauê W.
extern enum Option {
auto;
cool;
hard;
}

@:native("my.full.pack.to.Option") class MyOption
{
public static var auto = "auto";
public static var cool = "cool";
public static var hard = "hard";
}

Cheers! ; )

2011/9/24 benjamin Dubois <[hidden email]>
Sorry I'm not used to using metadata can you post an simple example please :)

On Sat, Sep 24, 2011 at 10:08 PM, Cauê Waneck <[hidden email]> wrote:
you can always use @:native metadata, as previously stated. Though I don't know if it will work on C++ because seems cpp doesn't like classes defined as externs

2011/9/24 benjamin Dubois <[hidden email]>
Is there an equivalent for the Neko target this hack 

enum Option {
       auto;
       cool;
       hard;
}
This will allow you to write Option.auto which will be an Option (and not a string, so it can't be used as String in haXe code).
Then since you want it to be a String _at runtime only_, you can declare the Option enum as "extern" and add somewhere in your code :

__js__('Option = { auto : "auto", cool : "cool", hard : "hard" };');
This will make sure that Option.auto == "auto"

Maybe using builtins ?

Ben


--

haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: enum questions

bubblebenj
Ok thanks I'll try that :)

On Sat, Sep 24, 2011 at 10:34 PM, Cauê Waneck <[hidden email]> wrote:
extern enum Option {
auto;
cool;
hard;
}

@:native("my.full.pack.to.Option") class MyOption
{
public static var auto = "auto";
public static var cool = "cool";
public static var hard = "hard";
}

Cheers! ; )


2011/9/24 benjamin Dubois <[hidden email]>
Sorry I'm not used to using metadata can you post an simple example please :)

On Sat, Sep 24, 2011 at 10:08 PM, Cauê Waneck <[hidden email]> wrote:
you can always use @:native metadata, as previously stated. Though I don't know if it will work on C++ because seems cpp doesn't like classes defined as externs

2011/9/24 benjamin Dubois <[hidden email]>
Is there an equivalent for the Neko target this hack 

enum Option {
       auto;
       cool;
       hard;
}
This will allow you to write Option.auto which will be an Option (and not a string, so it can't be used as String in haXe code).
Then since you want it to be a String _at runtime only_, you can declare the Option enum as "extern" and add somewhere in your code :

__js__('Option = { auto : "auto", cool : "cool", hard : "hard" };');
This will make sure that Option.auto == "auto"

Maybe using builtins ?

Ben


--

haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org