位语法:模式匹配中的默认大小?

时间:2017-02-11 00:11:05

标签: erlang

根据Erlang编程(第2版),p。的底部。 103:

  

Size的值指定段的大小。默认值   取决于类型。对于一个整数,它是8,对于一个浮点数是64,   对于二进制文件,它是二进制文件的大小。 在模式匹配中,   此默认值仅对最后一个元素有效

以下示例不反驳规则"模式匹配中的最后一个元素是唯一可以使用默认大小的元素":

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ImageDrawingFromOneClassToAnother {

    private JFrame frame;

    private JPanel pane;
    private JPanel leftPane;
    private JPanel rightPane;

    private ImageIcon icon;

    private JButton button;

    private JLabel label;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ImageDrawingFromOneClassToAnother().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        icon = new ImageIcon(this.getClass().getResource("king.png")); //Read images as if they were already embedded resources

        button = new JButton("Draw image");

        label = new JLabel(""); //Create an empty label

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                label.setIcon(icon); //On button click, we set the icon for the empty label
            }
        });

        pane = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 200); //Set a size for the main panel
            }
        };

        pane.setLayout(new GridLayout(1, 2)); //The main panel

        leftPane = new JPanel(); //The button panel
        leftPane.setLayout(new BoxLayout(leftPane, BoxLayout.PAGE_AXIS));

        leftPane.add(button);

        rightPane = new JPanel(); //The panel where the image will be drawn
        rightPane.add(label);

        //We add both (button and image) panels to the main panel
        pane.add(leftPane);
        pane.add(rightPane);

        frame.add(pane); //Add the main panel to the frame
        frame.pack(); //Calculate its preferred size
        frame.setVisible(true); //Set it to be visible
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

enter image description here重复编程Erlang(第二版)中所述的相同规则:

  

4.4默认值
  ...
  ...
  默认大小取决于类型。对于整数,它是8.对于浮点数   它是64.对于二进制,它是所有二进制文件。 在匹配中,这个   默认值仅对最后一个元素有效。 所有其他元素   匹配中的二进制元素必须具有大小规范。

1 个答案:

答案 0 :(得分:2)

好吧,既然我已经从文档中发布了这句话,我注意到它说:

  

在匹配中,此默认值仅对最后一个有效   元件。匹配中的所有其他 二进制 元素必须具有大小   说明书

事实上,我确实在这里收到错误:

7> f().
ok

8> Bin = <<97, 98, 99>>.            
<<"abc">>

9> <<B/binary, X/integer>> = Bin.   
* 1: a binary field without size is only allowed at the end of a binary pattern

错误消息比文档和编程Erlang(第二版)中的任何内容都要清晰。因此,在我看来应该重写文档,如下所示:

  

4.4默认值

     

[开头省略:&lt;&lt;&lt; 3.14&gt;&gt;甚至不是合法的语法。]

     

在构造中,默认大小取决于类型。对于整数,它是8.对于float,它是64.对于二进制,它是实际的大小   指定的二进制文件:

1> Bin = << <<97, 98, 99>>/binary, 17/integer, 3.2/float >>.  
<<97,98,99,17,64,9,153,153,153,153,153,154>>
 |<------>| ^|<-------------------------->|
  binary=24 |           float=64  
            |        
        integer=8

2> size(Bin). % Returns the number of bytes:
12            % 3*8 bits + 8 bits + 64 bits = 96 bits => 96/8 = 12 bytes
     

在匹配中,只允许在模式结尾处使用没有显式大小的二进制段,其默认大小是剩余的字节数。   比赛右侧的二进制文件:

25> Bin = <<97, 98, 99>>.
<<"abc">>

26> << X/integer, Rest/binary >> = Bin.
<<"abc">>

27> X.
97

28> Rest.
<<"bc">>
     

模式中具有二进制类型的所有其他段必须指定大小:

12> Bin = <<97, 98, 99, 100>>.         
<<"abcd">>

13> << B:1/binary, X/integer, Rest/binary >> = Bin. %'unit' defaults to 8 for  
<<"abcd">>                    %binary type, total segment size is Size * unit  

14> B.
<<"a">>

15> X.
98

16> Rest.
<<"cd">>

17> << B2/binary, X2/integer, Rest2/binary >> = Bin. 
* 1: a binary field without size is only allowed at the end of a binary pattern

我有这个权利吗?