What causes "ambiguous overloading on return type of function error" and how do I solve it? - minizinc

I have array of boolean and I am trying to find the first instance that is 'true' :
set of int: people = 1..5;
array[people] of bool: status=[false, false, true, true, false ];
var int: objective= min([p | p in people where status[p]])
and I get "ambiguous overloading on return type of function error"

Related

crystal: Unexpected token: "=" on simple assignment

in my application I have an array of elements of a custom class:
#input_signals = [] of Sig
The class looks like this:
class Sig
#name : String
#state = false
def initialize(command : String)
arguments = command.split(" ")
#name = arguments[1]
end
end
Now I just wanted to test whether crystal is pass-by-reference or pass-by-value.
So I wanted to change the value of the first element in the array like this:
#input_signals[0].#state = true
But I get the following error:
In interpreter.cr:23:34
23 | #input_signals[0].#state = true
^
Error: unexpected token: "="
I definetely know that the #state variable is visible, since the debug output works without a problem.
If I print out the variable in the same class I get the output like this:
Code:
p! #input_signals[0].#state
Output:
#input_signals[0].#state # => false
Do you know what could cause this problem?

"Value of optional type 'Set<String>?' must be unwrapped", but I didn't ask for an optional

Xcode complains "Value of optional type 'Set?' must be unwrapped to refer to member 'contains' of wrapped base type 'Set'"
Here's the function:
func talks_to (_ dialog_dict: Dictionary<String, Set<String>>, one: String, two: String) -> Bool {
return dialog_dict[one].contains(two)
}
This is a nested function, meant only to make logic clearer, and the parameters are guaranteed to be non-nil by the outer code. How do I get swift to understand this?
Any time you fetch an item from a dictionary using subscripting, the result is an Optional because the key you use might not be found. Sh_Khan gave you a nice elegant solution: (voted)
return dialog_dict[one]?.contains(two) == true
That works because nil does not equal true, but the compiler will unwrap it and check the value inside to see if it equals true if it's not nil. So if the result of dialog_dict[one] is nil or false, it does not equal true. Only if dialog_dict contains a value for the key one and that value is true does the expression return true.
Make it
return dialog_dict[one]?.contains(two) == true
or
return dialog_dict[one]!.contains(two)
this dialog_dict[one] returns optional

Optional vs Bound value assigning var from array

I want to check if there is a value in a array and if so assign to a String using a if-left statement:
if let scoreValue = scoreValueArray[element!]{
// do something with scoreValue
}
Error: Bound value in a conditional binding must be of optional type
So tried changing the ! to ? but error persists.
Any input appreciated.
scoreValueArray is an array of strings, where a String value is appended to array if a condition is met, then array is saved to NSUserdefaults.
So element is a int which corresponds to a index in the array, bt only if the index is occupied with a String, so
scoreValueArray[element!]
could return an 'Index out of bounds', hence want to use the if-let.
Although the accepted answer clearly puts why optional binding is not available in the current implementation, it doesn't provide with a solution.
As it is shown in this answer, protocols provide an elegant way of safely checking the bounds of an array. Here's the Swift 2.0 version:
extension Array {
subscript (safe index: Int) -> Element? {
return indices ~= index ? self[index] : nil
}
}
Which you can use like this:
let fruits = ["Apple", "Banana", "Cherry"]
if let fruit = fruits[safe: 4] {
// Do something with the fruit
}
It's not clear what type your scoreValueArray is, but for the sake of this answer, I'm going to assume it's an array of Int.
var scoreValueArray: Array<Int>
Now, if we look the definition of the Array struct, we'll find this:
struct Array<T> : MutableCollectionType, Sliceable {
// other stuff...
subscript (index: Int) -> T
// more stuff
}
So, calling the subscript method on our array (which is what we do when we say scoreValueArray) returns a non-optional. And non-optionals cannot be used in the conditional binding if let/if var statements.
We can duplicate this error message in a more simple example:
let foo: Int = 3
if let bar = foo {
// same error
}
This produces the same error. If we instead do something more like the following, we can avoid the error:
let foo: Int? = 3
if let bar = foo {
// perfectly valid
}
This is different from a dictionary, whose subscript method does return an optional (T?). A dictionary will return a value if the key passed in the subscript is found or nil if there is no value for the passed key.
We must avoid array-index-out-of-bounds exceptions in the same way we always do... by checking the array's length:
if element < scoreValueArray.count {
scoreValue = scoreValueArray[element]
}

How to fix 'Int' is not convertible to 'String' in a comparison that uses only Int?

Imagine my surprise that this Swift code generates an error in Xcode 6.1.1:
public func unlockNextLevel() -> Int
{
var highest : Int = 123
return (highest < highestUnlockedLevel)
}
More precisely, it tells me that in the return line:
'Int' is not convertible to 'String'
So, since I had some of these weird conversion errors before, I thought I'll try converting both types to Int and see what I get:
public func unlockNextLevel() -> Int
{
var highest : Int = 123
return (Int(highest) < Int(highestUnlockedLevel))
}
I then get the following error on the return line:
Could not find an overload for '<' that accepts the supplied arguments
But when I break it down to just constant values:
return (Int(3) < Int(12))
I get the int not convertible to string error again.
'Int' is not convertible to 'String'
Gnnnnn.... oh-kay, so I give it another shot without the brackets:
return highest < highestUnlockedLevel
This then gives me yet another error message:
Cannot invoke '<' with an argument list of type '(#lvalue Int, #lvalue Int)'
Okay, I get it Swift, I'm stoopid. Or maybe ... hmmm, take this, Swift:
var result = highest < highestUnlockedLevel
return result
Arrrr ... nope. Swift decides that now the return line constitutes yet another error:
'Bool' is not convertible to 'Int'
(... dials number for psych evaluation ...)
So ... could someone explain to me:
how to fix or work around this issue?
and of course: Why?? Oh, why???
Note: This is in a mixed Objc/Swift project if that makes any difference. The highestUnlockedLevel variable is declared in a Swift class with custom setter and getter.
(highest < highestUnlockedLevel) produces Bool not Int (unlike Objective-C where it returns int that can be automatically converted to BOOL), thats why you get the error. But certainly its wrong and misleading error as the problem is that Bool cannot be converted to Int.

Swift error: Int is not convertible to 'DictionaryIndex<Int,Int>'

When I switched over from Xcode 6 beta 4 to beta 5 I got this error (Int is not convertible to 'DictionaryIndex') in reference to my plays variable. I don't know how to remedy this.
Here is the code block:
var plays = Dictionary<Int,Int>()
var done = false
var aiDeciding = false
//Function that says what to do if a UIButton is clicked:
#IBAction func UIbuttonClicked(sender:UIButton) {
if !plays[sender.tag] && !aiDeciding && !done {
setImgforCircle(sender.tag, player:1)
thinkingLabel.hidden = true
thinkingLabel2.hidden = true
thinkingLabel3.hidden = true
}
I suppose this is just a deceiving error message and the actual error is in
if !plays[sender.tag]
Instead, it should be
if plays[sender.tag] != nil
This is due to a change in the language, specifically this
Optionals no longer implicitly evaluate to true when they have a value
and false when they do not, to avoid confusion when working with
optional Bool values. Instead, make an explicit check against nil with
the == or != operators to find out if an optional contains a value.
plays[sender.tag] returns an Option<Int> and you are assuming that a nil value will evaluate to false, hence !plays[sender.tag].
Since beta 5 this is forbidden and you have to explicitly check against nil
plays[sender.tag] != nil