1 package org.simantics.scl.compiler.commands;
3 import gnu.trove.map.hash.THashMap;
5 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
6 import org.simantics.scl.compiler.top.ExpressionEvaluator;
7 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
8 import org.simantics.scl.compiler.types.Type;
9 import org.simantics.scl.compiler.types.Types;
10 import org.simantics.scl.runtime.function.Function1;
12 public class ValueToStringConverter {
13 final RuntimeEnvironment environment;
14 final THashMap<Type, Function1> showInstances = new THashMap<Type, Function1>();
16 public ValueToStringConverter(RuntimeEnvironment environment) {
17 this.environment = environment;
20 public String show(Object value, Type type) throws SCLExpressionCompilationException {
21 Function1 showInstance = showInstances.get(type);
22 if(showInstance == null) {
23 showInstance = (Function1)new ExpressionEvaluator(environment, "show")
24 .expectedType(Types.function(type, Types.STRING))
26 showInstances.put(type, showInstance);
28 return (String)showInstance.apply(value);