ClojureDart icon indicating copy to clipboard operation
ClojureDart copied to clipboard

Case statement fails when used with a variable

Open TiLogic opened this issue 2 years ago • 5 comments

Case statement fails when used with a variable with the following error:

Execution error (AssertionError) at cljd.compiler/emit-case* (compiler.cljc:1600).
Assert failed: (and (symbol? expr) (env expr))

ClojureDart version: 1e38014dcb93dcdb230d125c7d0966556a82d234

Examples:


;; OK
(case :ios
  :ios "iOS"
  :android "Android"
  "not implemented")   => "iOS"

(defn case-test
  [os]
    (case os
      :ios "iOS"
      :android "Android"
      "not implemented"))

(case-test :ios)  => "iOS"

;; ERROR
(def os :ios)

(case os
  :ios "iOS"
  :android "Android"
  "not implemented")   => Execution error...

(let [the-os :ios]
    (case the-os
      :ios "iOS"
      :android "Android"
      "not implemented"))    => Execution error...

Currently working around this by using condp.

TiLogic avatar Apr 27 '22 18:04 TiLogic

Thanks for reporting, we'll take a look

dupuchba avatar May 02 '22 07:05 dupuchba

Is this case expression top level?

cgrand avatar May 18 '22 08:05 cgrand

Yes, it makes it easier to differentiate by platform.

e.g.

(ns app.presentation.reorderable-list-view
  (:require app.util.platform :as platform))

(defn android-reorderable-list-view
  [args]
  (f/widget
  ...))

(defn ios-reorderable-list-view
  [args]
  (f/widget
  ...))

(defn  macos-reorderable-list-view
  [args]
  (f/widget
  ...))

(def reorderable-list-view
  (case platform/os
    :android android-reorderable-list-view
    :ios ios-reorderable-list-view
    :macos macos-reorderable-list-view))

;; elsewhere
(:require [app.presentation.reorderable-list-view :refer [reorderable-list-view]])

TiLogic avatar May 18 '22 12:05 TiLogic

Currently compiles correctly. ~~However, case also fails in the following example:~~

(def a 1)

(defn broken []
  (case a
    1 "one"
    2 "two"
    "?"))
Compiling to Dart... @08:46
  repl.main
Execution error (AssertionError) at cljd.compiler/emit-case* (compiler.cljc:1616).
Assert failed: (and (symbol? expr) (env expr))

TiLogic avatar May 18 '22 12:05 TiLogic

Case statements are failing in the following scenario as well...

(defn acase
  [month]
  (case month
    dart:core.DateTime/may "May"
    dart:core.DateTime/june "June"
    "????"))

(defn acond
  [month]
  (condp = month
    dart:core.DateTime/may "May"
    dart:core.DateTime/june "June"
    "????"))

(dart:core/print (acase dart:core.DateTime/may))   ;=> ????
(dart:core/print (acond dart:core.DateTime/june))  ;=> June

TiLogic avatar May 25 '22 14:05 TiLogic