liquidsoap one line if statement

big balls of mud killer
, in 06 September 2016

I often run into this problem of trying to conditionally assign something in liquidsoap.

source = fallback(id="fallback",track_sensitive=false,

if tunein_metadata_updates_enabled == "true" then
  source = on_track(tunein.submit(partnerid="ppppid",partnerkey="ppppkey",stationid="s123456"),

Of course this doesn’t work. Liquidsoap complains that I didn’t use the result of the variable.

Since liquidsoap does not actually allow mutation of variables, what you are really doing in this case is creating two source variables. You are not using the second one. Somewhat like this:

source1 = fallback(id="fallback",track_sensitive=false,

if tunein_metadata_updates_enabled == "true" then
  source2 = on_track(tunein.submit(partnerid="ppppid",partnerkey="ppppkey",stationid="s123456"),

Maybe thinking of it this way, the error makes more sense. Of course you didn’t use the result of source2.

At line 18, character 8: The variable source defined here is not used anywhere
in its scope. Use ignore(...) instead of source = ... if you meant  to not use
it. Otherwise, this may be a typo or a sign that your script  does not do what
you intend.

The solution is to write an if else statement on one line.

source = if tunein_metadata_updates_enabled == "true" then
 partner_key,stationid=tunein_station_id), source) else source end

This assigns the result to source if the condition passes, if not assign the source to itself.

Modern Online Radio with Liquidsoap Book - Free Sample

Need more help with liquidsoap? Can’t get your script to work?

I wrote a book to help you learn Liquidsoap. The book covers all aspects of liquidsoap, from getting started, to making dynamic streams, audio processing, video, customizing metadata, authentication, and more. The book is available for purchase now here!

You can get a free sample chapter of my book! Just enter your email address to subscribe to my mailing list and I'll send you a free PDF sample of the book in return.