Switch pattern matching // Pattern is unused

414 views
Skip to first unread message

Jörg Wasmeier

unread,
May 12, 2013, 2:34:48 AM5/12/13
to haxe...@googlegroups.com
Hi there,

at the moment i recive always the message "Pattern is unused". I read the Documentaionn but i do not understand how i should modify my code to get it work. I would very happy if someone can give me a little hint.

    override public function partAdded(name:Dynamic):Void {
       switch(name){
           case thumb:
               this.click(clickHandler);
           case title:
               title.text = data.nameDe;
           case creator:
               creator.text = data.creator;
           case price:
               price.text = data.price + " CHF";
       }
   }

"name:Dynamic" can be anything sometimes a Button, sometimes a Textfield etc... all extend the same Basic class DomObject.


thanks for some hints.

Luca

unread,
May 12, 2013, 3:38:54 AM5/12/13
to haxe...@googlegroups.com
patterns must be constant values, that is to say, literals or inline-static vars

if an identifier is 'not' the name of a constant value, then 'case thumb:' actually means 'match against 'any' input, and put the value into 'thumb' where thumb is declared locally for the case block

eg:

function odds(n) {
   var ret = [];
   for (i in 0...n) if ((i%2)!=0) ret.push(i);
   return ret;
}
switch(odds(n)) {
   case []: trace("no odd values");
   case [x,3]: trace('first odd value is $x and second is 3");
   case result: trace('odd values were $result');
}

Simon Krajewski

unread,
May 12, 2013, 3:38:52 AM5/12/13
to haxe...@googlegroups.com
Haxe3 allows only constant patterns. "case thumb" is understood as a capture variable pattern, where thumb will hold the value of the matched expression. It indeed matches any pattern, and the subsequent patterns are then unused.

You can replace the switch with if...elseif...else, or employ reflection on name and switch e.g. on Type.getClass(name).

Simon

Jörg Wasmeier

unread,
May 12, 2013, 4:41:44 AM5/12/13
to haxe...@googlegroups.com
Okay but isn´t it a Performance killer if i use else if Statements than a "old fashioned switch" stament and "else if" stamtens ar not so readable than the an "old switch" stament. 

Perhapse it make sense to create a optional variable for the switch Statements like.

switch (name) ... Pattern matching 
switch (name, false) .. Old fashioned switch

Perhaps that will be a better solution then disable all the Pattern matching with the Compiler flag, because i love the idea behind Pattern matching but in this case it is a little bit counterproductive.

Simon Krajewski

unread,
May 12, 2013, 4:54:09 AM5/12/13
to haxe...@googlegroups.com
Am 12.05.2013 10:41, schrieb J�rg Wasmeier:
> Okay but isn�t it a Performance killer if i use else
> if Statements than a "old fashioned switch" stament and "else if"
> stamtens ar not so readable than the an "old switch" stament.

A switch on non-constant values could only lead to generated code that's
essentially equal to an if/elseif chain. Optimization at compile-time is
not possible, and I don't think a JIT compiler would be able to handle
it either.

There's a reason patterns have to be constant in (most) statically typed
languages, and it's not just to annoy users.

Simon

Jörg Wasmeier

unread,
May 12, 2013, 5:14:47 AM5/12/13
to haxe...@googlegroups.com
Alright i understand this point, but on the otherside it is true that with haxe (3) we must write for some cases larger if-elseif statments which is for the code readability not as good as an "old switch" or i can turn of the complete Pattern matching what perhaps can generate errors if i use extern libs.

Perhaps its just me that does not like large else if Statements.

On Sunday, May 12, 2013 10:54:09 AM UTC+2, Simon Krajewski wrote:
Am 12.05.2013 10:41, schrieb J�rg Wasmeier:
> Okay but isn�t it a Performance killer if i use else

Juraj Kirchheim

unread,
May 12, 2013, 5:30:01 AM5/12/13
to haxe...@googlegroups.com
You could:

1. Use guard conditions instead.
2. Use a macro (Jason posted one here just a couple of days ago)
3. Use a map:

var actions = [
thumb=> function () this.click(clickHandler);
title=> function () title.text = data.nameDe;
creator=> function () creator.text = data.creator;
price=> function () price.text = data.price + " CHF";
];
if (actions.exists(name)) actions.get(name)();

Note that this will probably not work with keys being Dynamic. I would assume that in this case all keys are object, so `name` should be at least of type `{}` (which would also permit usage of maps, i.e. ObjectMap).

Regards,
Juraj

Jörg Wasmeier

unread,
May 12, 2013, 5:39:41 AM5/12/13
to haxe...@googlegroups.com
Hi,

okay thanks for the Hint, i haven not see the previosue Thread about this Topic, so i Think the solution with the macro will work fine and all is good and everybody is happy.

thanks to all for the good conversation.
Reply all
Reply to author
Forward
0 new messages