fix(sampler): return None instead of panicking on parameter type mismatch in CompletedTrial::get()

This commit is contained in:
Manuel Raimann
2026-02-12 16:00:05 +01:00
parent 3d3dcb4c26
commit 12efbaabab
2 changed files with 28 additions and 12 deletions
+6 -12
View File
@@ -121,13 +121,9 @@ impl<V> CompletedTrial<V> {
/// Looks up the parameter by its unique id and casts the stored
/// [`ParamValue`] to the parameter's typed value.
///
/// Returns `None` if the parameter was not used in this trial.
///
/// # Panics
///
/// Panics if the stored value is incompatible with the parameter type
/// (e.g., a `Float` value stored for an `IntParam`). This indicates
/// a bug in the program, not a runtime error.
/// Returns `None` if the parameter was not used in this trial or if
/// the stored value is incompatible with the parameter type (e.g., a
/// `Float` value stored for an `IntParam`).
///
/// # Examples
///
@@ -150,11 +146,9 @@ impl<V> CompletedTrial<V> {
/// assert!((-10.0..=10.0).contains(&x_val));
/// ```
pub fn get<P: Parameter>(&self, param: &P) -> Option<P::Value> {
self.params.get(&param.id()).map(|v| {
param
.cast_param_value(v)
.expect("parameter type mismatch: stored value incompatible with parameter")
})
self.params
.get(&param.id())
.and_then(|v| param.cast_param_value(v).ok())
}
/// Returns `true` if all constraints are satisfied (values <= 0.0).