基本上我试图做的是使自己的IF函数但通用的返回类型是否可以登记具有通用返回类型Scala中的UDF
我想注册这个功能,所以我可以用一个数据帧
def myfunc[A] (cond : Boolean, condMet : A, condNotMet : A): A = {
if(cond)
return condMet;
else
return condNotMet;
}
val apply_func = udf(myfunc _);
val xp = h.withColumn("myUDF", apply_func('values0,"met",false).show
使用它,但我得到这个错误
<console>:100: error: type mismatch;
found : (Boolean, Nothing, Nothing) => Nothing
required: (Boolean, A2, A3) => RT
val apply_func = udf(myfunc _);
我想知道如果我能真正注册该功能,或者如果不是做同样的其他功能已经存在,或也许这是不可能的。
我大多会发送字符串,布尔值,数字作为condMet和condNotMet。
在您return'的'用法:https://stackoverflow.com/questions/12560463/return-in-scala/12560532 – Reactormonk
在你的例子,新列的类型应该是什么? –
我没有设置类型,它取决于具体的情况。这就是为什么我用它 –