Below is an updated version of the list_view.mc file. The error you're seeing (
list_view.mc:28,12: no viable alternative at input 'foreach(s in'
) can occur if the Monkey‑C compiler isn’t happy with how the loop variable is declared. In Connect IQ it’s best to explicitly declare the variable type (or use the var keyword) when using a foreach loop.
In the revised code below, we’ve replaced
foreach(s in _summits) {
...
}
with
foreach (var s in _summits) {
...
}
Below is the complete, updated list_view.mc file:
using Toybox.WatchUi;
using Toybox.Graphics;
using Toybox.Position;
using Toybox.System;
class SotaListView extends WatchUi.ScrollableMenu {
var _summits = [];
function initialize() {
ScrollableMenu.initialize();
}
function onShow() {
// Request a single GPS fix with a 15 second timeout
Position.enableLocationEvents(Position.LOCATION_ENABLE_TYPE_SINGLE, method(:_onFix), {
:timeout => 15000
});
}
function _onFix(info) {
if (info != null && info.hasKey(:position)) {
var lat = info[:position][:latitude];
var lon = info[:position][:longitude];
_summits = SummitParser.getNearest([lat, lon]);
// Build menu items array
var items = [];
foreach (var s in _summits) {
var dist = s.distanceTo([lat, lon]);
items.add({ :text => s.ref + " " + s.name, :subtext => dist.format("%0.1f") + " km" });
}
setMenuItems(items);
} else {
setMenuItems([{ :text=>"No GPS fix.", :subtext=>"Go outside 😊" }]);
}
WatchUi.requestUpdate();
}
// When the user selects a summit from the menu
function onMenuItem(menuItem) {
var idx = menuItem.getIndex();
if (idx < _summits.size()) {
return new SummitView(_summits[idx]);
}
return null;
}
}
Additional Notes
- Make sure you use
foreach (var s in _summits) instead of omitting the type for the variable.
- If you have similar errors in other modules (for example in
model.mc), ensure that each foreach loop uses the explicit var (or an appropriate type) declaration.
Try rebuilding your project with this version of list_view.mc. This should eliminate the “no viable alternative” error. If any other errors occur, please feel free to share them!