Code:
abstract class Protocol
end
# Base modbus protocol
abstract class ModbusProtocol < Protocol
end
# Base modbus protocol
class ModbusRtuProtocol < ModbusProtocol
end
# Base device info
abstract class DeviceInfo
end
# Base device info
class MeterDeviceInfo < DeviceInfo
# Device network number
getter networkNumber : Int32 = 1
end
# Base driver task executer
abstract class CollectorDriverExecuter(TDevice, TProtocol, TResponseType)
def initialize(@deviceInfo : TDevice, @protocol : TProtocol)
end
def initialize(@deviceInfo : TDevice, @protocol : TProtocol, &block : TResponseType -> _)
execute(&block)
end
# Execute and iterate values in block
abstract def execute(&block : TResponseType -> _)
end
# Base executer
abstract class BaseExecuter(TResponseType) < CollectorDriverExecuter(MeterDeviceInfo, ModbusRtuProtocol, TResponseType)
end
# Common executer with start session
abstract class CommonExecuter(TResponseType) < BaseExecuter(TResponseType)
# To implement
abstract def postExecute(&block : TResponseType -> _) : Void
# Start session and execute other
def execute(&block : TResponseType -> _) : Void
StartSessionExecuter.new(@deviceInfo, @protocol) do
postExecute(&block)
end
end
end
class StartSessionExecuter < BaseExecuter(Bool)
def execute(&block : Bool -> _)
data = Bytes[0xCC, 0x80, 0x00, 0x00, 0x00]
p data
end
end
StartSessionExecuter.new(MeterDeviceInfo.new, ModbusRtuProtocol.new) do
end
Error:
Error in line 60: instantiating 'StartSessionExecuter:Class#new(MeterDeviceInfo, ModbusRtuProtocol)'
in line 29: instantiating 'execute()'
in line 55: expanding macro
in /usr/lib/crystal/slice.cr:34: undefined constant T
{% if @type.name != "Slice(T)" && T < Number %}
^
Crystal 0.25.1 (2018-06-29)
LLVM: 6.0.0
Default target: x86_64-pc-linux-gnu
Reduced:
def execute
Bytes[1_u8]
end
execute
https://play.crystal-lang.org/#/r/4l2k
Error in line 5: instantiating 'execute()'
in line 2: expanding macro
in /usr/lib/crystal/slice.cr:34: undefined constant T
{% if @type.name != "Slice(T)" && T < Number %}
The error does not happen when Bytes.[] is called from to level scope or Slice.[] is used.
Duplicate of #5343
This can be closed now that #5354 is merged
I tested it, and it works.
Thanks.
Most helpful comment
Duplicate of #5343