Perl 6:maxpairs警告有关未定义值的字符串化

时间:2018-12-09 13:06:45

标签: perl6 rakudo

似乎maxpairs不喜欢在具有未定义值的列表上调用:

> my @foo; @foo[2] = 4; say @foo.maxpairs;
Use of uninitialized value of type Any in string context.
Methods .^name, .perl, .gist, or .say can be used to stringify it to something meaningful.
  in block <unit> at <unknown file> line 1

(2 => 4)

max没有相同的问题,似乎只是忽略了未定义的值:

> my @foo; @foo[2] = 4; say @foo.max;
4

相同的错误确实发生在:

> my @foo; @foo[2] = 4; say @foo.pairs.max(*.value)
Use of uninitialized value of type Any in string context.
Methods .^name, .perl, .gist, or .say can be used to stringify it to something meaningful.
  in block <unit> at <unknown file> line 1

2 => 4

因此,似乎只有在使用不带过滤器参数的max时,才会忽略未定义的值。

这是一个错误吗?

1 个答案:

答案 0 :(得分:6)

由于这看起来像是个错误,我已经用

修复了它
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

class PostIt extends JFrame {

    private enum Colors {
        YELLOW, BLUE, GREEN, ORANGE, VIOLET, RED;
    }

    // stuff
    private static final long serialVersionUID = 1L;
    private static int count = 0;
    private int color;

    // components
    private final JPanel topPanel;
    private final JButton minimize;
    private final JButton exit;
    private final JButton add;

    // coords and dimensions
    private static Point componentCoords = null;
    private static final int WIDTH = 200; // 30*6
    private static final int HEIGHT = 200; // 30*7
    private static final int ORIGINALX = 700;
    private static final int ORIGINALY = 100;
    // 7*6 matrix
    // 30*30 px each cell
    private static int animationH = 10;

    public PostIt() {
        // frame
        environment();
        count++;

        // panel
        final JPanel panel = new JPanel();
        panel.setBounds(0, 0, WIDTH, animationH);

        panel.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(final MouseEvent e) {
            }

            @Override
            public void mouseDragged(final MouseEvent e) {
                final Point currentCoords = e.getLocationOnScreen();
                setLocation(currentCoords.x - componentCoords.x, currentCoords.y - componentCoords.y);
            }
        });
        panel.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(final MouseEvent e) {}

            @Override
            public void mousePressed(final MouseEvent e) {
                componentCoords = e.getPoint();
            }

            @Override
            public void mouseReleased(final MouseEvent e) {
                componentCoords = null;
            }

            @Override
            public void mouseEntered(final MouseEvent e) {}

            @Override
            public void mouseExited(final MouseEvent e) {}
        });

        // top panel
        topPanel = new JPanel((LayoutManager) null);
        topPanel.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(final MouseEvent e) {
            }

            @Override
            public void mouseDragged(final MouseEvent e) {
                final Point currentCoords = e.getLocationOnScreen();
                setLocation(currentCoords.x - componentCoords.x, currentCoords.y - componentCoords.y);
            }
        });
        topPanel.setBackground(randomColor());
        topPanel.addMouseListener(new MouseListener() {
            @Override
            public void mousePressed(final MouseEvent e) {
                componentCoords = e.getPoint();
            }

            @Override
            public void mouseClicked(final MouseEvent e) {

            }

            @Override
            public void mouseEntered(final MouseEvent e) { // expand
                new Timer(1, e1 -> {
                    panel.setSize(WIDTH, animationH);
                    animationH += 1;
                    if (animationH >= 30) {
                        ((Timer) e1.getSource()).stop();
                    }
                }).start();
            }

            @Override
            public void mouseExited(final MouseEvent e) { // compress
                new Timer(1, e1 -> {
                    panel.setSize(WIDTH, animationH);
                    animationH -= 1;
                    if (animationH <= 10) {
                        ((Timer) e1.getSource()).stop();
                    }
                }).start();
            }

            @Override
            public void mouseReleased(final MouseEvent e) {
                componentCoords = null;
            }
        });
        add(topPanel);
        panel.setBackground(topPanel.getBackground().darker());
        topPanel.add(panel);

        // exit
        exit = new JButton("x");
        exit.addActionListener(e -> {
            count--;
            dispose();
        });
        // topPanel.add(exit);

        // add
        add = new JButton("+");
        add.addActionListener(e -> {
            final PostIt p = new PostIt();
            System.out.println(count);
        });
        // topPanel.add(add);

        // add
        minimize = new JButton("_");
        minimize.addActionListener(e -> {
            setState(JFrame.ICONIFIED);
        });
        // topPanel.add(minimize);
    }

    public static boolean isMouseWithinComponent(final Component c)
    {
        final Point mousePos = MouseInfo.getPointerInfo().getLocation();
        final Rectangle bounds = c.getBounds();
        bounds.setLocation(c.getLocationOnScreen());
        return bounds.contains(mousePos);
    }

    private void save() {
    }

    private void restore() {
    }

    /*
     * private void loadImages () { try { exitIcon = new ImageIcon(ImageIO.read(new
     * File("src\\media\\exit48.png"))); addIcon = new ImageIcon(ImageIO.read(new
     * File("src\\media\\add48.png"))); minimizeIcon = new
     * ImageIcon(ImageIO.read(new File("src\\media\\minimize48.png"))); } catch
     * (Exception e) { e.printStackTrace(); } }
     */

    private Color randomColor() {
        switch (Colors.values()[randInt(Colors.values().length - 1)]) {
        case YELLOW:
            return new Color(255, 240, 120);
        case BLUE:
            return new Color(200, 240, 250);
        case GREEN:
            return new Color(205, 255, 140);
        case ORANGE:
            return new Color(250, 190, 5);
        case RED:
            return new Color(240, 140, 130);
        case VIOLET:
            return new Color(215, 175, 250);
        default:
            return null;
        }
    }

    private void environment() {
        setBounds(nextX(), nextY(), WIDTH, HEIGHT);
        setAlwaysOnTop(true);
        setUndecorated(true);
        setResizable(false);
        setVisible(true);
    }

    private static int randInt(final int min, final int max) {
        if (min < 0 || min > max || max <= 0)
            throw new IllegalArgumentException();
        return (int) (java.lang.Math.random() * (max - min + 1) + min);
    }

    private static int randInt(final int max) {
        return randInt(0, max);
    }

    private int nextX() {
        return randInt(ORIGINALX - 50, ORIGINALX + 50);
    }

    private int nextY() {
        return randInt(ORIGINALY - 50, ORIGINALY + 50);
    }

    public static void main(final String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                new PostIt();
            } catch (final Exception ex) {
                ex.printStackTrace();
                // Logger.getLogger(PostIt.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
    }
}

其中也照顾“ minpairs”。