-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Haskell's where in switch structure #582
Comments
We used to have this, it was removed in 1.2 |
The above example wouldn't have worked with the old |
This could potentially be very powerful with more complicated checkData = (data) ->
for own key, field of data
[ key
field
match field
| sep '@' => 'user'
| sep '#' => 'topic'
| _ => 'none'
where sep = -> (field.indexOf it) != 0] which would compile to something similar to: var own$ = ().hasOwnProperty;
var checkData = function(data) {
var key, field, results$ = [];
for (key in data) {
if (own$.call(data, key)) {
field = data[key];
results$.push([key, field, fn$(field)]);
}
}
return results$;
function fn$(field){
function sep(it){
return field.indexOf(it) !== 0;
}
switch (false) {
case !sep('@')(field):
return 'user';
case !sep('#')(field):
return 'topic';
default:
return 'none';
}
}
}; I know that the above example is technically equivalent to the following, but it's a bit cleaner IMO, even in this theoretical (and potentially not the best) use case. checkData = (data) ->
sep = -> (field.indexOf it) != 0
for own key, field of data
[ key
field
match field
| sep '@' => 'user'
| sep '#' => 'topic'
| _ => 'none' ] |
Here's a specific real-world case I found can simplify my own code drastically: # original
checkMatch = (node, format) ->
type = typeof format
if typeof node != type
return false
switch type
| 'function' => format node
| 'object' =>
(if Array.isArray format
checkArray
else
checkObject) node, format
| _ => format == node
# with this proposed syntax
checkMatch = (node, format) ->
| type != typeof node => false
| type == 'function' => format node
| type == 'object' =>
(if Array.isArray format
checkArray
else
checkObject) node, format
| _ => format == node
where type = typeof node They should compile nearly identical. <aside> function checkMatch(node, format) {
var type = typeof format;
if (typeof node !== type)
return false;
else if (type === 'function')
return format(node);
else if (type === 'object')
return (Array.isArray(format) ? checkArray : checkObject)(node, format);
else
return node === format;
} </aside> |
I would suggest the following syntax (in a hopefully self-explanatory context-free grammar):
If something isn't quite clear, or if I've screwed something up, I'm all ears. |
So is this moving anywhere? I am curios, why was the "where" structure removed from the livescript, what was the reasoning? it is pretty useful as for me. |
I get the feeling this is something waiting on a patch I don't have time to write at the moment. |
@gkz @vendethiel What do you think of this syntax? I'm aware it deviates from the old I don't have time to write a patch for this currently, but I may experiment with the compiler later on, and this would be interesting. What do you all think? Is there anything I missed or made a mistake on in the idea itself? |
It doesn't really matter to me. The syntax in the first post is gonna be a huge issue, tho. We're reaching the limits of implicit blocks, I think. |
It will be very good if we can have
where
in switch structures, like occurs in Haskell, like we use inthat
.The possible LiveScript syntax could be:
That would compile to:
This would be awesome!
The text was updated successfully, but these errors were encountered: